将设置类添加到UWP应用

编程入门 行业动态 更新时间:2024-10-22 11:34:06
本文介绍了将设置类添加到UWP应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发通用Windows平台应用程序,但Visual Studio中没有设置模板.

I'm developing a Universal Windows Platform app but there is no Settings template in Visual Studio.

如何实现一个简单的,类型强且可观察的类,将我的设置存储在LocalSettings或RoamingSettings中?

How can I implement an easy, strongly typed and observable class that stores my settings in LocalSettings or RoamingSettings?

推荐答案

  • 创建一个继承自ObservableSettings的新类.
  • 调用基类构造函数,以指示是否要将设置存储在 LocalSettings 或 RoamingSettings 中.
  • 添加所有调用基类成员的属性 在getter和setter中设置和获取.无需传递属性名称或使用 nameof()运算符!
  • (可选)您可以设置一个默认值,以使用 DefaultSettingValue 属性装饰该属性.
  • Create a new class inheriting from ObservableSettings.
  • Call to the base class constructor indicating if you want to store the settings in LocalSettings or in RoamingSettings.
  • Add all your properties calling the base class members Set and Get in the getter and in the setter. No need to pass the name of the property or use nameof() operator!
  • Optionally you can set a default value decorating the property with DefaultSettingValue attribute.
  • 以下是设置类的示例:

    namespace Test { public class Settings : ObservableSettings { private static Settings settings = new Settings(); public static Settings Default { get { return settings; } } public Settings() : base(ApplicationData.Current.LocalSettings) { } [DefaultSettingValue(Value = 115200)] public int Bauds { get { return Get<int>(); } set { Set(value); } } [DefaultSettingValue(Value = "Jose")] public string Name { get { return Get<string>(); } set { Set(value); } } } }

    ,以及如何在您的app.xaml中添加实例:

    and here how to add an instance in your app.xaml:

    <Application x:Class="Test.App" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:local="using:Test" RequestedTheme="Light"> <Application.Resources> <local:Settings x:Key="settings"/> </Application.Resources> </Application>

    以MVVM方式访问和修改值:

    Access and modify the values in a MVVM fashion:

    <Page x:Class="Test.MainPage" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:local="using:Test" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" mc:Ignorable="d" DataContext="{StaticResource settings}"> <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="Bauds"/> <TextBox Text="{Binding Default.Bauds, Mode=TwoWay}"/> <TextBlock Text="Name"/> <TextBox Text="{Binding Default.Name, Mode=TwoWay}"/> </StackPanel> </Page>

    所有内容都将正确存储在您的设置存储库中.

    Everything will be stored properly in your settings repository.

    在这里,您已经实现了 DefaultSettingValue 和 ObservableSettings :

    Here you have the implementation of DefaultSettingValue and ObservableSettings:

    using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Reflection; using System.ComponentModel; using Windows.Storage; [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public sealed class DefaultSettingValueAttribute : Attribute { public DefaultSettingValueAttribute() { } public DefaultSettingValueAttribute(object value) { Value = value; } public object Value { get; set; } } public class ObservableSettings : INotifyPropertyChanged { private readonly ApplicationDataContainer settings; public ObservableSettings(ApplicationDataContainer settings) { this.settings = settings; } public event PropertyChangedEventHandler PropertyChanged; protected bool Set<T>(T value, [CallerMemberName] string propertyName = null) { if (settings.Values.ContainsKey(propertyName)) { var currentValue = (T)settings.Values[propertyName]; if (EqualityComparer<T>.Default.Equals(currentValue, value)) return false; } settings.Values[propertyName] = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); return true; } protected T Get<T>([CallerMemberName] string propertyName = null) { if (settings.Values.ContainsKey(propertyName)) return (T)settings.Values[propertyName]; var attributes = GetType().GetTypeInfo().GetDeclaredProperty(propertyName).CustomAttributes.Where(ca => ca.AttributeType == typeof(DefaultSettingValueAttribute)).ToList(); if (attributes.Count == 1) return (T)attributes[0].NamedArguments[0].TypedValue.Value; return default(T); }

    您可以从我在 GitHub 中创建的存储库中下载带有功能示例的解决方案.

    You can download a solution with a functional example from the repository I created in GitHub.

    更多推荐

    将设置类添加到UWP应用

    本文发布于:2023-11-06 13:43:49,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1563853.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:UWP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!