Xamarin Software Back Button Event - c#

How can I fire an event when the software back button in the navigation bar is tapped?
I tried using the following code but this only works for a hardware back button.
protected override bool OnBackButtonPressed()
{
return true;
}

The OnBackButtonPressed is fired when you press the back button on the device. If you want to get backbutton event. You may need to create Custom Renderers for each platform. You can refer to the answer here Override nav bar back button click on Xamarin.Forms

Related

Is there a way to disable back or forward navigation in Webview2 winforms

In webview2, setting source property to Uri for navigating.
Let's say First URL is opened in webview2 then navigated to another URL.
With the back button on Right-click context menu, able to navigate to the first page.
From google search, found there is no direct way to disable back and forward as of now.
In the normal system forms browser, performed an approach like below which is working
added a bool variable(like IsMyNavigationCall), setting it to true whenever just before navigating to some URL
Added a check in NavigationStarted event and if it's false(when navigation triggered from actions like back) cancelling the request and resetting the bool variable.
In Webview2, it's not working. The problem is navigation is not cancelled even after setting CoreWebView2NavigationStartingEventArgs.cancel to true.
Is there any way or kind of hack to prevent navigation between the back and forward?
It seems that you're looking for CoreWebView2.HistoryChanged Event. In order to enable/disable a "Back" button and a "Forward" button when a new URL is navigated to in WebView2, try the following:
Given:
WebView2 control: webView21
Back button: btnBack
Forward button: btnForward
//subscribe to CoreWebView2 events (add event handlers)
webView21.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
...
private void CoreWebView2_HistoryChanged(object sender, object e)
{
btnBack.Enabled = webView21.CoreWebView2.CanGoBack;
btnForward.Enabled = webView21.CoreWebView2.CanGoForward;
}
You can disable the context menu and the accelatorkeys via CoreWeView2.Settings
https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#arebrowseracceleratorkeysenabled
https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.1245.22#aredefaultcontextmenusenabled
Example:
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;

windows silverlight app stuck on one page

In my windows phone app I am doing a scanning process, wherever I am in the app, when the scan button is pressed on my bottom appbar, I navigate to my scan page, known as ScanView.xaml.
Problem:
I have overridden onnavigatedTo method of scanView to initiate scanning screen.
When I press back button while I am on the scanning screen It goes back to ScanView.xaml and hence onnavigatedTo method is again called and my scanning screen again appears.
What I want to do is, when I press back button while on scanning screen it should navigate directly back to my mainpage.xaml
P.S : I have tried overriding the backbutton handler, but it is still not working.
Here is my code.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//this method is invoked as soon as we are navited to the ScanView
Scanning();
}
Thanks in advance
Just remove ScanView.xaml from the navigation stack (it is the item at the top of the stack) when navigated to the scanning page.

UWP Xbox One page navigation when using WebView

Usually when running a UWP app on Xbox the B button on the controller is handled automatically and will return you to the previous page.
I have a page which contains a WebView, when you use the directional buttons to place the focus box around that control, the B button no longer responds. You can use the A button to take control of the WebView and display the pointer and the B button then will return focus back as above but I cannot navigate back using the B button until you move the focus box to a different control. This also happens using AdControl since this uses WebView.
I have tried to capture KeyDown:
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
Debug.WriteLine(args.VirtualKey.ToString());
}
This responds with GamePadB, GamePadX etc but not when the focus box is around the WebView.
Is there anyway I can find out when the GamePad buttons (specifically B) are pressed when the focus box is around the WebView (or AdControl) and the control isn't engaged so I can manually invoke the backstack navigation?
Since this issue happens when using the XY focus mode for the app, if your OS version is 14393 or higher, one workaround for this issue is to use the mouse mode for this page which contains the webview by setting RequiresPointer="WhenFocused" as following:
<Page RequiresPointer="WhenFocused">
...
</Page>
And set another page to XY focus mode by using the following code in the app.xaml.cs:
this.RequiresPointerMode =
Windows.UI.Xaml.ApplicationRequiresPointerMode.WhenRequested;
For more information, please try to refer to the following article:
https://msdn.microsoft.com/en-us/windows/uwp/input-and-devices/designing-for-tv#mouse-mode

On windows forms, set tab focus to a button

I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}

How to create Accept and Cancel Buttons in C#, Visual Studio

I am making a Windows Form Application.
I have two buttons (Accept and Cancel) and I want to call the click event for one when Enter is pressed, and the click event for the other when Escape is pressed.
This is a code I've tried (I found it here on a similar question) that didn't work.
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter)) {
this.btnOrder.PerformClick();
}
if (e.KeyCode.Equals(Keys.Escape))
{
this.btnCancel.PerformClick();
}
}
I have looked through all the properties and events of the buttons but I cannot find anything Accept or Cancel related. I know that this is probably way too easy but I've learnt of these buttons tonight and I just can't do them.
Can someone tell me what am I doing wrong?
It's a property of the Form:
AcceptButton
CancelButton
In the Form Designer: You add the buttons to the form, select the form, and select the buttons you want as accept and/or cancel button.

Categories

Resources