gpt4 book ai didi

win-universal-app - UWP 页面状态管理

转载 作者:行者123 更新时间:2023-12-02 07:06:26 25 4
gpt4 key购买 nike

我想学习如何管理导航之间的页面状态。例如,导航到第 1 页,然后导航到第 2 页,但是当我导航回第 1 页时,UI 元素必须已经存在并且具有与以前相同的数据,并且不得重新初始化它们或不得再次绑定(bind)数据编译器。另外我可以做些什么来管理整个应用程序的状态,这样我就终止了应用程序,然后当我下次启动它时,与上次相同的状态已经存在。我可以将它应用于整个应用程序吗?或者如果我只想在几页上应用它怎么办?任何帮助将不胜感激。

最佳答案

or example a navigate onto page1 and then i navigate to page2, but when i navigate back to page1, the UI elements must already be there with the same data as before and they must not be re-initialized or data must not be binded again by the compiler.

对于这个问题,你可以使用UIElement.CacheMode propertyFrame.CacheSize property . CacheSize 属性设置导航历史中可以为框架缓存的页面数,CacheMode 属性设置一个值,指示呈现的内容应缓存为复合内容尽可能使用位图。

我们知道,UWP 应用程序默认使用 rootFrame 来显示多个页面,我们只需使用 Navigation 方法来更改框架中的内容。您可以在空白 UWP 应用的 OnLaunched(LaunchActivatedEventArgs e) 方法中看到这一点。但是如何实现缓存功能呢?例如,您的应用有两个页面和一个根框架。您可以在 OnLaunched(LaunchActivatedEventArgs e) 方法中定义 CacheSize 属性,例如:

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;
}

Also what I can do to manage state of whole application such that, I terminate the app and then when i launch it next time, the same state is already there as last time. can i apply it on whole application?

对于这个问题,您需要使用 Frame.GetNavigationState methodOnSuspending(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 元素。

关于win-universal-app - UWP 页面状态管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com