我想学习如何管理导航之间的页面状态。 例如,导航到page1然后我导航到page2,但是当我导航回page1时,UI元素必须已经存在与之前相同的数据,并且它们不能重新初始化或者数据不能再被绑定到编译器。 我还可以做什么来管理整个应用程序的状态,这样,我终止应用程序,然后当我下次启动它时,相同的状态已经存在于上次。我可以在整个申请中申请吗?或者如果我只想在几页上应用它怎么办?任何帮助将不胜感激。
答案 0 :(得分:6)
或示例a导航到page1然后我导航到page2,但是当我导航回page1时,UI元素必须已经存在与之前相同的数据,并且它们不能重新初始化或数据不能是由编译器再次绑定。
对于此问题,您可以使用UIElement.CacheMode property和Frame.CacheSize property。 CacheSize
属性设置导航历史记录中可以为帧缓存的页数,CacheMode
属性设置一个值,指示在可能的情况下应将渲染内容缓存为合成位图。
众所周知,UWP应用默认使用rootFrame
来显示多个页面,我们只需使用Navigation
方法来更改框架中的内容。您可以在空白UWP应用的OnLaunched(LaunchActivatedEventArgs e)
方法中看到此信息。但是如何实现缓存功能呢?例如,您的应用有两个页面和一个根框架。您可以在CacheSize
方法中定义OnLaunched(LaunchActivatedEventArgs e)
属性,例如:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
...
// Ensure the current window is active
rootFrame.CacheSize = 2;
Window.Current.Activate();
}
然后在你的两个页面的构造函数中启用CacheMode
属性,例如:
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Enabled;
}
我还可以做些什么来管理整个应用程序的状态,以便我终止应用程序,然后当我下次启动时,相同的状态已经存在于上次。我可以在整个申请中申请吗?
对于此问题,您需要使用Frame.GetNavigationState method在OnSuspending(object sender, SuspendingEventArgs e)
方法中保存页面状态,并且可以将此状态保存到应用程序的本地设置中。例如:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
Frame rootFrame = Window.Current.Content as Frame;
string navstate = rootFrame.GetNavigationState();
var localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["nav"] = navstate;
deferral.Complete();
}
如何检索此信息?您可以覆盖OnLaunched(LaunchActivatedEventArgs e)
方法,首先,您需要判断您的应用上次,用户或系统使用ApplicationExecutionState enumeration的关闭方式,例如:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
//#if DEBUG
// if (System.Diagnostics.Debugger.IsAttached)
// {
// this.DebugSettings.EnableFrameRateCounter = true;
// }
//#endif
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
//rootFrame.Navigate(typeof(MainPage), e.Arguments);
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated ||
e.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
object value;
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.TryGetValue("nav", out value))
{
rootFrame.SetNavigationState(value as string);
}
else
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
}
else
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
}
// Ensure the current window is active
rootFrame.CacheSize = 2;
Window.Current.Activate();
}
但要注意,当应用关闭时,下次启动此应用时,UI元素将重新初始化,此功能只能在您上次关闭应用时导航到该页面,但数据在该页面将丢失。但您也可以将数据保存到本地设置,当您导航到页面时,将值设置为这些UI元素。