UWP - 保存 xaml 页面的属性 (Slider/Combobox)

UWP - Save attributes of a xaml page (Slider/Combobox)

我遇到了以下问题:在我的 xaml 主页面中,我有一个按钮可以导航到另一个名为设置的页面,该页面还包括应用程序的设置。我跳转页面的方法如下:

this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(Settings::typeid));

如您所知,我希望 UI-Elements (Comboboxes/Sliders) 保存它们的属性以供下次使用。甚至不是为了应用程序的下一次启动,只是为了设置页面上的下一次跳转。如果我从设置返回主页并再次返回设置,组合框和滑块的值将被重置。我需要为我的程序逻辑保存它们。

在我的 c++ 桌面应用程序中,我使用注册表项来保存它,但这在 uwp 中不太可能,也不是最简单的解决方案。在 Java 中,我会使用一个全局静态变量来保存值并绑定到 UI 元素。我如何在 UWP 中实现它?

您可以通过 Page.NavigationCacheMode Property 实现。这样 属性 您就可以将页面状态存储在应用程序的缓存中。
您可以在页面的 xaml-tag 中使用它,也可以在页面的代码隐藏的构造函数中设置。您可以找到属性 here.

的可能值

示例-XAML:

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

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Grid>
</Page>

示例 CS:

 public sealed partial class SettingsPage : Page
    {
        public SettingsPage()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Enabled;
        }
    }