I have a Popup which needs to be light dismissed when user taps an element outside the popup. By default, pressing the Esc key also triggers LightDismiss, which I would like to prevent. Instead, I'd like to handle this KeyDown event explicitly.
Handling the Tapped event in the page's root to check if user's tapped outside the popup is one solution, but since every tap in my app will go through this, I'd prefer not to take this approach.
Some pseudo-code:
<Page>
<Grid>
<Popup
x:Name="FooTip"
Opened="FooTip_Opened"
Closed="FooTip_Closed"
IsLightDismissEnabled="True"
ShouldConstrainToRootBounds="True">
<!-- Popup content here -->
</Popup>
</Grid>
</Page>
TLDR: FooTip should not get light dismissed when Esc key is pressed.
FooTip should not get light dismissed when Esc key is pressed.
You could handle the PreviewKeyDown event for the Page like this:
private void OnPreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape
&& FooTip.IsOpen)
{
e.Handled = true;
}
}
Related
I added a button component in my application, and I am expecting it to change the color when user hovers on it or click it.
But looks like that's not the default behavior of the button.
Is there a way to get that behavior maybe by setting some attribute?
You can simply use a event handler to set the color of the button when clicking it.
Here's the code sample below for your reference:
<Button Clicked="Button_Clicked">
Code in backend:
private void Button_Clicked(object sender, EventArgs e)
{
var button = (Button)sender;
button.BackgroundColor = Color.FromArgb("#FFA07A");
}
Update:
I found a simialr issue for your scenerio in Github, you can follow it up here:https://github.com/dotnet/maui/issues/9552
I've developed an app, as soon as the user enters a username and password and continues I display a dialog with a selection of buttons for the user to choose. As there is a login screen, the keyboard is displayed. I've hooked into the Enter button so that it is the same as tapping the Sign In button - this is where I hit issues.
IF the dialog is shown whilst the keyboard is active, the dialog content size is not full screen (despite the keyboard disappearing). But if the Sign In button is tapped (i.e. keyboard is not active) everything is fine.
The images below probably explain things better.
I'm not sure what to do to resolve this - any ideas?
FYI - I can scroll the buttons but only in that top section of screen.
As always - I ask a question and then find the answer.
When I catch the "Enter button tapped" event, I set focus to the page. It's a bit of a hack as I'd rather know why the issue is happening and stop it altogether instead of just dealing with it, but it works...
this.Focus(FocusState.Keyboard);// this is the line that solves it.
Full code:
private void tbPassword_KeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
this.Focus(FocusState.Keyboard);
StartLogin();
}
}
#Rick have a good way.But I have other good way that you can make the keyboard show and UWP can arrange the UI.
You can use InputPane.GetForCurrentView().TryHide() to hide the keyBoard and use InputPane.GetForCurrentView().TryShow() to show the keyBoard. So you can hide it when you want to show the ContentDialog.
But I have not think it is a good way.
You can use InputPane.GetForCurrentView().Showing to know when is the keyBoard is Showed and use InputPane.GetForCurrentView().Hiding to know when hide the keyBoard .
First,you can make the Grid with a row is show keyBoard when show keyBoard it will get the Hight.
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition x:Name="HightKeyboard" Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
When the keyBoard be showed ,you can use e.OccludedRect.Height get the keyBoard's height.
InputPane.GetForCurrentView().Showing += (s, e) =>
{
HightKeyboard.Height=new GridLength(e.OccludedRect.Height);
};
And you should make the row height hide when hide the keyBoard.
InputPane.GetForCurrentView().Hiding += (s, e) =>
{
HightKeyboard.Height=new GridLength(1);
};
You can show the UI in the first Row ,and when the keyBoard show the height above will be arrange.
I'm trying to declare a button as default in UWP app but receive an error:
The property 'IsDefault' was not found in type 'Button'
How can I make a default button in UWP app?
I down know what IsDefault is in WPF but to get if a button is pressed in UWP you can use CoreWindow.GetForCurrentThread().KeyDown. Create a Method that will be called from when the button is pressed or VirtualKey.Enter is clicked.
public MainPage()
{
this.InitializeComponent();
CoreWindow.GetForCurrentThread().KeyDown += MainPage_KeyDown; ;
}
private void MainPage_KeyDown(CoreWindow sender, KeyEventArgs args)
{
switch (args.VirtualKey)
{
case Windows.System.VirtualKey.Enter:
// handler for enter key
break;
default:
break;
}
}
You can use key down event which you can place on any textbox for example if you are making a login page then probably there will be 2 textboxes for username and password then just add key down event handler to textbox as it will be the last mandatory field like this:
<PasswordBox KeyDown="PasswordKeyDown"/>
then you can handle this event as:
using System.Windows.Input;
private void PasswordKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
Login();
}
Hope it will help you :)
There is not easy or clean way to solve your problem because IsDefault is not available for uwp apps.
if you are using MVVM or you want to reuse your code I recommend you to use Behaviors and follow the examples that the other guys posted.
I need a Button which user can invoke by pressing the ENTER key.
In an UWP app, by default a Button can be invoked by pressing the Enter key. So I guess what you want is setting the focus on this Button when there are some other UIElements in your page.
You can refer to Keyboard navigation among UI elements,
By default, the tab order of controls is the same as the order in which they are added to a design surface, listed in XAML, or programmatically added to a container.
To focus on the Button which is not the first element, you can just give the TabIndex="1" property to your Button, this property can make your Button get focus whenever the page is loaded, but if you change the focus on other controls in this page, you will need to reselect this button by mouse clicking, touching or TAB key.
Is there a way to scroll-up view the required amount when the soft-keyboard shows up? The page in my app has a lot of textboxes and when the keyboard shows up, a lot of them hide under it and the user has to manually scroll down/hide keyboard to enter values in the other textboxes.
How can I scroll-up the page by some amount to improve UX?
You can have a KeyDown event set for each textbox and inside event handler check if Enter key is pressed shift focus to next textbox.
KeyDown="txtMessage1_KeyDown"
private void txtMessage1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
nextTextbox.Focus();
}
}
In Win 8, the flipview control is a great control to browse the collection. But how or what is the best way to make a "selection" with a tap or a mouse click? I can always put a button outside of the flip view, but that's not the touch experience that everyone of a tablet would expect.
can someone give some example code (XAML/C#) of how to setup a flipview control with a selection of some sort that would navigate to a totally different page?
I wrote some sample code that works, if I'm understanding the question correctly. I am able to swipe through the FlipView and tap the individual item:
<FlipView Tapped="FlipView_Tapped_1">
<Image Source="Images/Apple.jpg" />
<Image Source="Images/Orange.jpg" />
<Image Source="Images/Banana.jpg" />
</FlipView>
And then
private YourTypeHere SelectedItem;
private void FlipView_Tapped_1(object sender, TappedRoutedEventArgs e)
{
this.SelectedItem = (sender as FlipView).SelectedItem;
}
You might not want to set a field, but you get the idea. Hopefully, you will be setting something in your view model. From there you can nav away or anything you need. A FlipView inherits from ItemsControl just like every other XAML repeater. So you can treat it exactly the same. http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx