Hiding/Showing text in Xamarin Forms - c#

I am working to develop a mobile application using Xamarin Forms. I would like to make certain text appear when a button is pushed in the application, and otherwise be hidden. However, I am not sure how to do this using XAML and C#. I tried writing a conditional, with a Console.WriteLine() command, but the text does not show in the application. Are there any suggestions on how to solve this problem, or what resources I should look into to learn more about this?
XAML Code - Buttons:
<Button Text="I am new to this app" Clicked="Button_Clicked">
</Button><Button Text="I have used this app before" Clicked="Button_Clicked_1"></Button
C# Code for these buttons:
/* This only changes the label of the buttons, it doesn't make the text appear separately like I want it to.*/
void Button_Clicked (System.Object sender, System.EventArgs e)
{
((Button)sender).Text = "Hey there! Welcome to this awesome app!";
}
void Button_Clicked_1(System.Object sender, System.EventArgs e)
{
((Button)sender).Text = "Welcome back!";
}

You need to use Labels to display the additional text
<Button x:Name="Button1" Text="I am new to this app" Clicked="Button_Clicked" />
<Label x:Name="Label1" Text="Hey there! Welcome to this awesome app!" IsVisible="False" />
<Button x:Name="Button2" Text="I have used this app before" Clicked="Button_Clicked_1" />
<Label x:Name="Label2" Text="Welcome back!" IsVisible="False" />
then in the code behind you can change the IsVisible property of each label
void Button_Clicked (System.Object sender, System.EventArgs e)
{
Label1.IsVisible = true;
}
void Button_Clicked_1(System.Object sender, System.EventArgs e)
{
Label2.IsVisible = true;
}

Related

How can I capture WPF events from a Winforms app

I have an WPF User control which is is hosted in an Elementhost. I use elementhost to include an WPF user control in my classical Windows forms app.
Now, from Windows forms side I am trying to capture the mouseDown event that is produced in an WPF label but I don't know how to do it.
Any ideas?
A case might be able to help you. The winform form calls the wpf control.
Create a WPF custom control. The xaml code of the control is as follows.
<Grid>
<Image Margin="10,10,10,90" x:Name="img" Stretch="Uniform" Opacity="1">
<Image.BitmapEffect>
<DropShadowBitmapEffect Opacity="1" />
</Image.BitmapEffect>
</Image>
<TextBox Background="Transparent" Foreground="White" Height="40" FontSize="32" Margin="44,0,56,36" x:Name="txtBox1" Opacity="0.5" Text="" VerticalAlignment="Bottom" /> </Grid>
You need to add the corresponding function to set the effect. The code is as follows.
public void SetSource(string fileName)
{
img.Source = new BitmapImage(new Uri(fileName) );
}
public void SetOpacity(double opacity)
{
img.Opacity = opacity;
}
//
public string GetText()
{
return txtBox1.Text;
}
Create a Winform application and add a reference, otherwise the control will not work properly. The list of references is pictured below.
Regenerate the solution. On the left toolbar, a WPF control appears and drag it to the form.
Use the button control in the winform project to call the corresponding function.
private void button1_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetSource(#"C:\Users\Admin\Pictures\Saved Pictures\9837f99502eba3d01d4fb671cab20c15.jpg");
}
private void button2_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetOpacity(0.5);
}
private void button3_Click(object sender, EventArgs e)
{
string text = ((UserControl1)elementHost1.Child).GetText();
label1.Text = text;
}
Test items: The left side is the traditional Winform control. The right side is the imported WPF control. You can clearly see the "translucent" effect of the picture.
Not sure what exactly you're trying to achieve. Below is a simple example.You can edit the MouseDown event of the UserControl as needed.
If there is a problem, please make your problem clearer and show me the complete code sample that can reproduce your problem for analysis.
UserControl:
<Grid>
<Label x:Name="label" Content="Label" MouseDown="label_MouseDown" Background="AliceBlue" Width="300" Height="200" />
</Grid>
private void label_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("hello");
}
Add the UserControl reference in the WinForms project, drag and drop the UserControl on the Form1 designer after rebuilding the WinForms project.
The result of running the project and clicking the Label in the UserControl is shown in the figure.

Xamarin.Forms unhide keyboard on lost focus on Android

I develop chat application on Xamarin.Forms.
I need to don't hide keyboard when I press Send button and hide keyboard when I tap anywhere else.
I made it for iOS.
Can I make the same for android?
Found a very simple method to achieve this goal, you can simply place a Button on the top of an Entry like this:
<Grid VerticalOptions="End">
<Entry x:Name="MessageEntry" TextChanged="MessageEntry_TextChanged_1" />
<Button x:Name="SendButton" Text="Send" HorizontalOptions="End" Clicked="Button_Clicked" IsEnabled="False" />
</Grid>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
MessageEntry.Text = null;
}
private void MessageEntry_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (MessageEntry.Text != null)
SendButton.IsEnabled = true;
else
SendButton.IsEnabled = false;
}
Tested on Android 6.0 emulator, works fine to me, you can customize the Button to make it looks more beautiful in this view:
you can have in your PCL a scrollview element in which you add you entry element, when you tap on the entry element it will bring the keyboard and not hide it, it only hide it when tapped on a different portion of the screen. let me know if it help

How do I make a Popup included in it's parent Control for the sake of mouse events?

I have a Control that contains a Popup. I am trying to close the Popup whenever someone clicks outside of the Control. This is the part of my code that sets up the problem:
AddHandler(Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseuttonEventHandler(HandleOutsideClick), true);
Now whenever I click in the Popup it causes PreviewMouseDownOutsideCapturedElementEvent to be raised. Am I misunderstanding this event? Is there something that I can do to have the Popup be considered a part of the Control so that it doesn't raise this event?
Does this work?
<Popup Name="Pop" LostFocus="ClosePop"/>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
Put the XAML code in your .xaml page and the C# code in the related .xaml.cs file.
Note: You may need to put the focus on the popup before this works, it may be done automatically; I haven't done this on popups, but I have done it on other objects.
Update: This works for me, clicking in the TextBox that says Test1 opens the Popup, and clicking in the TextBox labeled Test2 closes it:
<Grid Background="White">
<StackPanel>
<TextBox Foreground="Black" LostFocus="ClosePop" GotFocus="OpenPop" Height="50">Test1</TextBox>
<TextBox Foreground="Black" Height="50">Test2</TextBox>
</StackPanel>
<Popup Name="Pop" Height="50" Width="50">
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center">Pop!</TextBlock>
</Popup>
</Grid>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = false;
}
private void OpenPop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = true;
}

show a usercontrol in wpf window by button

I have user control and wanna show it by clicking a button in a window designed by wpf.
I made a control user project and referenced it in a wpf project in this way:
xmlns:myproj="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
and in <Grid>tag I have this :
<myproj:UserControl1 Visibility="Hidden"
x:Name="customproj" />
and as I told I have a button in the main window in wpf project:
<Button Content="click me" HorizontalAlignment="Left" Margin="186,127,0,0" VerticalAlignment="Top" Width="124" Height="31" Click="Button_Click"/>
but I don't know how to write the event of the Button_Clickto open the control user.
private void Button_Click(object sender, RoutedEventArgs e)
{
//I don't know what to write!!!
}
I searched a lot but didn't find a suitable answer for my problem!
thank you
Just set the Visibility of UserControl to Visible:
private void Button_Click(object sender, RoutedEventArgs e)
{
customproj.Visibility = Visibility.Visible;
}

Moving back button to app bar

I'm currently writing a Windows Store App (XAML/C#). I'm trying to move the back button into the top app bar.
I've tried moving the code for the back button (default code from the BasicPage template) into a AppBar. However, it doesn't bring me back to the previous page when I click it.
This is the code which I moved into the app bar:
<Button x:Name="backButton"
Margin="39,59,39,0"
Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
VerticalAlignment="Top"
AutomationProperties.Name="Back"
AutomationProperties.AutomationId="BackButton"
AutomationProperties.ItemType="Navigation Button"/>
In TopAppBar, the NavigationHelper.GoBackCommand is invalid. You can add Click method to your Button. just like this:
<Button ... Click="backBtn_Click"/>
in C#:
private void backBtn_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
Frame.GoBack();
}

Categories