Xamarin.Forms unhide keyboard on lost focus on Android - c#

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

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.

Hiding/Showing text in Xamarin Forms

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;
}

How to create an onClick method for a button that will send the user to the bottom of the page in Xamarin?

In my Xamarin mobile app I'm trying to give a button the functionality to send the view to the bottom of the page whenever the button is clicked. As of right now, the button reveals hidden entries and the user must scroll down to see the entries revealed. I want it to snap to the bottom of the page so the user won't have to scroll.
Thanks!
You could put your content into a ScrollView,and then use ScrollToAsync method to scroll to the position of your entry which you want display.
For example :
<ScrollView x:Name="scroll">
<StackLayout Orientation="Vertical">
<Button Clicked="Button_Clicked" Text="ToBottom"></Button>
....... //your content
<Entry x:Name="entry" Placeholder="entry"></Entry>
</StackLayout>
</ScrollView>
in your page.xaml.cs :
private void Button_Clicked(object sender, EventArgs e)
{
scroll.ScrollToAsync(entry,ScrollToPosition.Start, false);
}

WPF Popup vs Tooltip

I have seen a lot of debate about when to use tooltip and when to use popup but I don't know which one is better for my case.
I have a button. When I click on it, the popup panel will appear and it has a lot of text and a small image (so it will be a quite big panel). The panel must stay there until I move my cursor OFF THE BUTTON (it must still close when the cursor is still on the panel but off the button).
<Button Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="mypopup">
<stuff>
</Popup>
private void clicked(object sender, RoutedEventArgs e) {
mypopopup.isopen = true;
}
private void mouseleaved(object sender, MouseEventArgs e) {
mypopup.isopen = false;
}
This is where I got to so far. The problem is that sometimes, the Popup appears on top of the button (which blocks the view of the button and so MouseLeave event kicks off, and Popup instantly disappears). I want the Popup to stay until i move the cursor away off the button.
So I did some google, and I think Tooltip may avoid this problem. But how to get Tooltip to appear on button click and not button hover?
Which one is better for me? Tooltip or Popup?
EDIT
I think I was not too clear with my question. I am asking which one i should use- Tooltip vs Popup based on MY SPECIFIC SITUATION (paragraph 2) and not in general. I think Popup is the right one to use but I have problems with using it (paragraph 3). so my question is can I solve this problem with Popup or should I use Tooltip better for this?
But how to get Tooltip to appear on button click and not button hover?
Handle the Click event for the Button and set the IsOpen property of the Popup to true:
private void Button_Click(object sender, RoutedEventArgs e)
{
popup1.IsOpen = true;
}
<Popup x:Name="popup1" StaysOpen="False">
<TextBlock>popup content...</TextBlock>
</Popup>
<Button Click="Button_Click" Content="op" />
Which one is better for me? Tooltip or Popup?
Popup is preferable whenever you want to customize the behaviour in any way.
Edit: If I understand your issue correctly, this should work:
<Button x:Name="button" Content="Button" Click="clicked" MouseLeave="mouseleaved"/>
<Popup Name="popup" PlacementTarget="{Binding ElementName=button}" StaysOpen="True" MouseLeave="mouseleaved">
<Border Background="Yellow">
<TextBlock>contents...</TextBlock>
</Border>
</Popup>
private void clicked(object sender, RoutedEventArgs e)
{
popup.IsOpen = true;
}
private void mouseleaved(object sender, MouseEventArgs e)
{
if (!button.IsMouseOver && !popup.IsMouseOver)
popup.IsOpen = false;
}

Simulate button click in a Metro Style App

I am in the process of building a metro style app and I need to know if there is a way to fire button clicks programmatically.
I have this PasswordBox and button in my xaml:
<PasswordBox IsPasswordRevealButtonEnabled="True" KeyDown="On_text_KeyDown" x:Name="SomePW" FontSize="50" KeyDown="On_text_KeyDown" Margin="0,0,0,190" Height="67" Width="363"/>
<Button Background="White" x:Name="Button_Go" Foreground="Black" Margin="20,0,0,190" Content="Go" FontSize="20" Click="Go_click" Height="67" Width="60"/>
And in my C# code this is the function that handles the key press in the PasswordBox:
private void On_text_KeyDown(object sender, RoutedEventArgs e)
{
KeyEventArgs K = (KeyEventArgs)e;
if (K.Key == Windows.System.VirtualKey.Enter)
{
//<TO-DO> Simulate Button Click Here
}
}
The problem is I can't seem to find a way to simulate the button click... Can someone help please?
Is there a problem with simply calling this or are you looking for a generic way to invoke a Click event on any button, perhaps to automate it?
Go_click(this, new RoutedEventArgs());
You can try this:
ButtonAutomationPeer peer = new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv = peer.GetPattern( PatternInterface.Invoke ) as InvokeProvider;
invokeProv.Invoke();
Another option is the following:
SomeButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Hope this helps!!

Categories