【问题标题】:Binding XAML element to Singleton class members将 XAML 元素绑定到 Singleton 类成员
【发布时间】:2019-12-17 14:07:19
【问题描述】:

所以我要离开这个网站:Winsows 10 UWP: How to Read and Save Setting Easily - Edi.Wang

我尝试过使用

 <Page.Resources>
    <core:AppSettings x:Key="AppSettings"/>
 </Page.Resources>

但我得到一个错误

Type 'AppSettings' is not usable as an object because it is not public or does not define a public parameterless constructor or a type converter.

这是我作为单例实现的 AppSettings 类,示例中没有。

using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.Storage;

namespace MediaManager.Services
{
    /// <summary>
    /// Singleton class for handing application settings data.
    /// </summary>
    public class AppSettings : INotifyPropertyChanged
    {
        private static volatile AppSettings _instance;

        private ApplicationDataContainer _localData = null;
        private ApplicationDataContainer _roamingData = null;

        private static object syncRoot = new object();

        private AppSettings()
        {
            _localData = ApplicationData.Current.LocalSettings;
            _roamingData = ApplicationData.Current.RoamingSettings;
        }

        public static AppSettings Instance
        {
            get
            {
                if (_instance is null)
                    lock (syncRoot)
                        if (_instance is null)
                            _instance = new AppSettings();

                return _instance;
            }
        }

        private void SaveSettings(string key, object value, bool roaming = false)
        {
            if (roaming)
                _roamingData.Values[key] = value;
            else
                _localData.Values[key] = value;
        }

        private T ReadSetting<T>(string key, T defaultValue = default(T), bool roaming = false)
        {
            if (roaming)
            {
                if (_roamingData.Values.ContainsKey(key))
                    return (T)_localData.Values[key];
            }
            else if (_localData.Values.ContainsKey(key))
                return (T)_localData.Values[key];

            return defaultValue;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged([CallerMemberName]string propName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }

        // List all setting here
        public string movie_staging_folder
        {
            get => ReadSetting<string>(nameof(movie_staging_folder), roaming: true);
            set
            {
                SaveSettings(nameof(movie_staging_folder), value, true);
                NotifyPropertyChanged();
            }
        }
    }
}

我知道就像错误所说的那样,我的 appSettings.cs 没有公共构造函数;这是一个单例类。我不知道如何让数据绑定与单例类一起工作。

【问题讨论】:

  • 为什么一定要单例?
  • @Stuart 根据我的阅读,NotifyPropertyChanged 如果不是单例则无法正常工作,因为如果存在多个 AppSetting 实例会出现并发症。

标签: c# xaml data-binding uwp singleton


【解决方案1】:
<TextBlock Text="{x:Bind local:AppSettings.Instance.MySetting, Mode=OneWay}" />
public class AppSettings
{
    private AppSettings()
    {
    }

    public static AppSettings Instance { get; } = new AppSettings();

    public string MySetting => "My setting";
}

【讨论】:

  • 我在名为 services 的文件夹中有 AppSettings,所以我在绑定语句中添加了 xlmns:services="using:MediaManager.Services" 并用 services: 替换了 local:。我认为这是正确的。我还使用了Mode=TwoWay,因为我想存储对我的应用程序用户设置所做的更改。当我打开应用程序时,文本字段为空白;正如我在第一次运行时所期望的那样。但是当我更改 TextBox 的值时,当 TextBox 失去焦点时它会恢复为空。
  • 原因很可能在AppSettings 类中。我看到的一个问题是ReadSettingroamingtrue 时从_localData 获取价值。
  • 非常感谢。我上次评论的问题是由于本地/漫游混淆。我一直在想我对我不太确定的代码做错了什么,我不认为回去确保我熟悉的代码是正确的。我需要更加小心复制/粘贴; newb 一路走错。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 2015-02-02
  • 2023-03-06
  • 1970-01-01
  • 2011-07-31
相关资源
最近更新 更多