简体   繁体   English

将XAML元素绑定到Singleton类成员

[英]Binding XAML element to Singleton class members

So I'm going off this site: Winsows 10 UWP: How to Read and Save Setting Easily - Edi.Wang 所以我要离开这个网站: Winsows 10 UWP:如何轻松读取和保存设置-Edi.Wang

I tried using 我尝试使用

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

but I get an error 但我得到一个错误

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.

This is my AppSettings class that I implemented as a singleton, where the one given in the example is not. 这是我作为单例实现的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();
            }
        }
    }
}

I understand that just like the error says, my appSettings.cs has no public constructor; 我知道就像错误所说的那样,我的appSettings.cs没有公共构造函数。 it's a singleton class. 这是一个单例课程。 I can't figure out how to get the data binding to work with a singleton class. 我不知道如何使数据绑定与单例类一起工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM