I am trying to to add an OnGotFocus event on Template 10 Back button as:
In PageViewModels.cs:
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
SystemNavigationManager.GetForCurrentView().BackRequested += OnGotFocus;
}
private async void OnGotFocus(object sender, BackRequestedEventArgs e)
{
....
}
But that does not work. Can anyone give me any pointers?
What you did in your code snippet is that you wired up a handler for the BackRequested event. This event is fired when the system registers a request to go back in the app. This can be fired by the user tapping the back button in Task bar in Tablet mode on desktop, or by clicking the back button in title bar of your app in Windowed mode or pushing the back button on mobile device.
Either way, this event is fired by the system and the only thing it does is that it calls your method. The name of your method does not matter at all.
I think you should review some basics about event handling in C# to clear up any confusion.
To be able to use the OnGotFocus event, you will have to create your own back button in XAML and add the handler to this button, because only this way you have full control over the control. If you just use the system provided BackRequested event, the system is in control and apart from this event you can't customize anything.
<Button GotFocus="OnGotFocus" Content="My back button" />
Related
In WPF I have a Window_KeyDown event which changes the mouse cursor shape when the user presses shift. It works as expected except if I have clicked on another app. So I added a Window_MouseEnter event like this to grab keyboard focus when the mouse reenters my app:
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
IInputElement b = Keyboard.Focus(this);
this.Focus();
Debug.WriteLine(b + DateTime.Now.ToLongTimeString());
}
I can see the MouseEnter event firing (with the debug line) when the mouse enters the app but my app still doesn't get keyboard events until I click in the app.
It's irritating because the mouse cursor changes properly when the mouse enters controls within my app so one would expect things to work but the shift-mouse functions don't work until after a click.
What am I missing?
I found that the secret is the Activate() method. I put it in the MouseEnter function which has a side-effect of forcing the entire app to show if some of it was hidden by other apps.
With Activate(), the Focus() method is not needed.
I'm trying to create a simple uwp app where I require to display a context menu, if user right clicks on non-touch device and if user holds on touch screen device.
I'm using righttapped event which works perfectly on my desktop. The other side, I use holding event which should be triggered when screen is long pressed but it doesn't work(on touch screen mobile).
private void Webview_Holding(object sender, HoldingRoutedEventArgs e)
{
Webview.ContextFlyout.ShowAt(sender as FrameworkElement);
}
Do I need to use some other event for showing context menu, if yes then which one?
You should use RightTapped event for both touch and non-touch devices as I answered here. However, WebView does not support all of the touch or keyboard events.
Remarks section of MSDN for WebView control:
As indicated in the Events table, WebView doesn’t support most of the
user input events inherited from UIElement, such as KeyDown, KeyUp,
and PointerPressed. A common workaround is to use InvokeScriptAsync
with the JavaScript eval function to use the HTML event handlers, and
to use window.external.notify from the HTML event handler to notify
the application using WebView.ScriptNotify.
I have a listview in which I display objects with their state and name. The state is surrounded by a button and when I click on it the state changes from active to inactive or the other way round.
Now I want to change the state when I click the mouse button down and change it back when the mouse button goes up. My button has to event "PreviewMouseLeftButtonDown" and "PreviewMouseLeftButtonUp" as you can see in the code below. In the SendStateMessage() method the state is send to a SCADA-Server and the server sends me the new state back and the GUI edits the change.
Normally it doesn't make any problems, but when I click to fast the "PreviewMouseLeftButtonUp" event doesn't get called. So the "MouseUp" is not shown at the consol.
I tried to call a Task.Delay() in the first mouseDown event because i thougt that the method needs some more time but this didn't work well. So i want to ask you if somebody has an idea what the problem could be and a way how this issue can be fixed.
Thanks for your time.
private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//_CurrentItem is an object of the class VM on which the click is executed.
_CurrentItem = sender as FrameworkElement).DataContext as VM
_CurrentItem.SendStateMessage();
}
private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("MouseUp");
}
EDIT:
After a bit of time passing by I had another idea to solve the problem, which is also working. I only wait for the MouseDownEvent and if this gets triggered i have a loop where i wait as long as the mouse is pressed, when the mouse is released i could leave the loop and there i inserte the code which i wanted to do in the MouseUPEvent.
You can also wait for the MouseDownEvent and if this gets triggered make a loop where you wait as long as the mouse is pressed, when the mouse is released you could leave the loop and inserte there your code which you wanted to do in the MouseUpEvent.
In my case another control consumed the mouseup event. So I catch the mouseup event on level of the top most windows.
I'm using JeffWilcox QR control (QR Scanner), but I have one issue with this control. When I launched a page with this control, it's working all the time, even I click back button and I am in main menu. Do you know how can I handle situation like: if user click back button the QR Scanner control will stop working ?
Working:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
nameOfControl.StopScanning();
base.OnNavigatingFrom(e);
}
When you're navigating through pages, there are some events triggered when you enter or exit a page, such as OnNavigatedFrom (when you left the page) or OnNavigatingFrom (just before leaving the page) or OnNavigatedTo (when you entered a page).
You can find more information about page navigation events in the documentation.
You can take advantage of these events to solve your issue. As it seems, create a handler for the OnNavigatingFrom event in the page where you have the QR control, to stop it.
In my windows phone 7 application, i have created a grid(named as Holder), and i have created a GotFocus event. So i need an if statement that, if the grid is touched by the user, i need a message box to be didplayed. Can anybody help me? Thanks in advance for your help!
In my app, i have web browser control, in that if the user GotFocus on the browser control, then i want to hide an image. But Tap event is not working for the browser control. Need help?
The event you should use is the Tap Event, it's meant for user interaction on Windows Phone.
Hook up the event with your grid in XAML:
<Grid x:Name="Holder" Tap="Holder_Tap">
</Grid>
And in your code:
private void Holder_Tap(object sender, GestureEventArgs e)
{
MessageBox.Show("This message");
}
It's not equal to the GotFocus event, but I think this will fit your needs: use the Tap event, it will be fired when the user taps the grid.