如何在不重新加载页面的情况下使用UWP NavigationView后退按钮

时间:2019-11-20 22:48:26

标签: uwp

我是UWP的新手。我正在将WPF应用程序迁移到UWP。我当前的问题是导航。当我转到某个页面并尝试返回上一页时,页面将重新加载。如何避免这种行为?我需要回到上一页,就像调用第二页时一样。 预先感谢您的帮助。

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

<Grid>
    <NavigationView x:Name="nvvMain" PaneDisplayMode="LeftCompact" IsBackEnabled="True" IsSettingsVisible="False" BorderThickness="1" IsPaneOpen="False" BackRequested="NvvMain_BackRequested">
        <NavigationView.MenuItems>
            <NavigationViewItem x:Name="nviHistory" Icon="Clock" Content="Histórico" />
            <NavigationViewItem x:Name="nviDictionary" Icon="Library" Content="Dicionário" />
            <NavigationViewItem x:Name="nviHelp" Icon="Help" Content="Ajuda" />
            <NavigationViewItem x:Name="nviSettings" Icon="Setting" Content="Configurações"  Tapped="NviSettings_Tapped" />
            </NavigationView.MenuItems>
        <Frame x:Name="NavigationFrame" />
    </NavigationView>
</Grid>

    public MainPage()
    {
        this.InitializeComponent();

        NavigationFrame.Navigate(typeof(SpeakPage));
    }

    private void NviSettings_Tapped(object sender, TappedRoutedEventArgs e)
    {
        NavigationFrame.Navigate(typeof(SettingsPage));
    }

    private void NvvMain_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
    {
        NavigationFrame.GoBack();
    }

1 个答案:

答案 0 :(得分:2)

默认情况下不缓存页面内容和状态,当您返回上一页时,它将变为初始状态。因此,如果要缓存信息而不重新加载,则需要在不想重新加载的页面中启用它。有关此的更多信息,您可以参考此document

public MyPage()
{
    this.InitializeComponent();
    this.NavigationCacheMode = NavigationCacheMode.Enabled;
}