【问题标题】:UWP - Save attributes of a xaml page (Slider/Combobox)UWP - 保存 xaml 页面的属性(滑块/组合框)
【发布时间】:2017-05-30 15:33:52
【问题描述】:

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

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

您可以理解,我希望 UI 元素(组合框/滑块)下次保存它们的属性。甚至不用于应用程序的下一次启动,仅用于设置页面的下一次跳转。如果我从设置返回主页并再次返回设置,组合框和滑块的值将被重置。我需要为我的程序逻辑保存它们。

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

【问题讨论】:

    标签: xaml user-interface uwp c++-cx


    【解决方案1】:

    您可以使用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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 2018-06-10
      • 2017-04-17
      • 2019-09-23
      • 2012-05-15
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多