UWP中的导航问题 [英] Navigation Issue in UWP

查看:82
本文介绍了UWP中的导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl;博士

我有3个页面MainPage.xamlBlankPage1.xamlBlankPage2.xaml,其中MainPageBlankPage1上的Button分别导航到BlankPage1BlankPage2.我启用了系统后退按钮,以便可以返回上一页.

I have 3 Pages MainPage.xaml, BlankPage1.xaml, BlankPage2.xaml with Button on MainPage and BlankPage1 that navigates to BlankPage1 and BlankPage2 Respectively. I enabled System Back Button so that i can go back to previous page.

Button轻按MainPage导航至BlankPage1,而Button轻按BlankPage1导航至BlankPage2.效果很好.

Button Tap on MainPage navigates to BlankPage1 and Button Tap on BlankPage1 navigates to BlankPage2. This works fine.

问题:当我在BlankPage2上点击Back Button时,它又回到了BlankPage.现在,当我点击BlankPage1上的Button时,它将转到BlankPage2,但是当我点击Back Button而不是转到BlankPage1时,它将直接导航至MainPage.

Problem: When I tap Back Button on BlankPage2 it goes back to BlankPage. Now when I tap the Button on BlankPage1 it goes to BlankPage2 but when i Tap the Back Button, instead of going to BlankPage1 it navigates directly to MainPage.

下面是我的代码.

MainPage.xaml

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="GoTo Page 1" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Button_Tapped"/>
    </Grid>
</Page>

MainPage.xaml.cs

using Windows.UI.Xaml.Controls;

namespace App2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            Frame.Navigate(typeof(BlankPage1));
        }
    }
}

BlankPage1.xaml

<Page
    x:Class="App2.BlankPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="GoTo Page 2" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Button_Tapped"/>
    </Grid>
</Page>

BlankPage1.xaml.cs

using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App2
{
    public sealed partial class BlankPage1 : Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                SystemNavigationManager.GetForCurrentView().BackRequested += (s, a) =>
                {
                    if (Frame.Content.GetType() == typeof(BlankPage1))
                    {
                        if (Frame.CanGoBack)
                        {
                            Frame.GoBack();
                            a.Handled = true;
                        }
                    }
                };
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
            }
        }

        private void Button_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            Frame.Navigate(typeof(BlankPage2));
        }
    }
}

BlankPage2.xaml

<Page
    x:Class="App2.BlankPage2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="Final Page" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Page>

BlankPage2.xaml.cs

using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace App2
{
    public sealed partial class BlankPage2 : Page
    {
        public BlankPage2()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                SystemNavigationManager.GetForCurrentView().BackRequested += (s, a) =>
                {
                    if (Frame.Content.GetType() == typeof(BlankPage2))
                    {
                        if (Frame.CanGoBack)
                        {
                            Frame.GoBack();
                            a.Handled = true;
                        }
                    }
                };
            }
            else
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
            }
        }
    }
}

推荐答案

每次导航到页面时,都会调用OnNavigatedTo方法,并且正在为BackRequested事件注册新的处理程序,这意味着当您按下返回按钮时,将执行多次.您应该在每个页面的OnNavigatedFrom方法中退订该事件.

Every time you navigate to a page, the OnNavigatedTo method is being called and you're registering a new handler for the BackRequested event, meaning it'll execute multiple times when you press the back button. You should unsubscribe to that event in your OnNavigatedFrom method of each page.

设置Handled = true并不意味着不会执行该事件的其他订阅,而只是表示:

Setting Handled = true doesn't mean that other subscriptions for that event won't be executed, it just means:

如果未将事件标记为已处理,则系统会决定是离开该应用程序(在移动设备系列上)还是忽略该事件(在台式机设备系列上).

If you don't mark the event as handled, the system decides whether to navigate away from the app (on the Mobile device family) or ignore the event (on the Desktop device family).

这篇关于UWP中的导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆