Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

c# - Handling Back Navigation Windows 10 (UWP)

In my Xaml Page I've got a Frame.

I'm trying to have a backButton event to just navigate inside frame .

so I tried to use this piece of code

public MainPage(){
    this.InitializeComponent();
    if(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) {
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
}
private void HardwareButtons_BackPressed(object sender,BackPressedEventArgs e) {
    if(insideFrame.CanGoBack())insideFrame.GoBack();
    else  Application.Current.Exit();
}

but In phone after doing HardwareButtons_BackPressed event it close the application.

It seems to running some default back button behavior on MainPage...

How can I fix it? And In Windows10 does they add new events to handle back navigation?


[Update]

Now I found out it's better to Use SystemNavigationManager in Windows 10 instead of Input.HardwareButtons.BackPressed.

SystemNavigationManager currentView = SystemNavigationManager.GetForCurrentView();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Windows 10 (UWP) include SystemNavigationManager in Windows.UI.Core namespace for Navigation purpose only.

Because SystemNavigationManager is part of Windows Universal Platform, So, it's supported by all device family running on Windows 10 including Mobile and PC.

For Single Page


If you just want to handle navigation for single page. Follow the following steps

Step 1. Use namespace Windows.UI.Core

using Windows.UI.Core;

Step 2. Register back request event for current view. Best place for this is main constructor of class after InitializeComponent().

public MainPage()
{
    this.InitializeComponent();
    //register back request event for current view
    SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
}

Step 3. Handle BackRequested event

private void Food_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
        e.Handled = true;
    }
}

For Complete Application at one place for single rootFrame


Best place for handling all backbutton for all Views is App.xaml.cs

Step 1. Use namespace Windows.UI.Core

using Windows.UI.Core;

Step 2. Register back request event for current view. Best place for this is OnLaunched just before Window.Current.Activate

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
    Window.Current.Activate();
}
    

Step 3. Handle BackRequested event

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

References- Handle back button pressed in UWP

Hope this is helpful to someone!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...