问题描述
tl;博士
我有3个页面MainPage.xaml
,BlankPage1.xaml
,BlankPage2.xaml
,其中MainPage
和BlankPage1
上的Button
分别导航到BlankPage1
和BlankPage2
.我启用了系统后退按钮,以便可以返回上一页.
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:
这篇关于UWP中的导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!