将 XAML 元素绑定到 Singleton class 成员
Binding XAML element to Singleton class members
所以我要离开这个网站: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 class,示例中给出的不是单例。
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 没有 public 构造函数;这是一个单身人士 class。我不知道如何让数据绑定与单例一起工作 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";
}
所以我要离开这个网站: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 class,示例中给出的不是单例。
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 没有 public 构造函数;这是一个单身人士 class。我不知道如何让数据绑定与单例一起工作 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";
}