[英]“Setting” class in C#
i was reading a tutorial for windows phone 7 using C# and sliverlight and i found this line 我正在使用C#和sliverlight阅读Windows Phone 7的教程,我发现了这一行
public static class Settings
{
public static readonly Setting<bool> IsRightHanded =
new Setting<bool>("IsRightHanded", true);
public static readonly Setting<double> Threshold =
new Setting<double>("Threshold", 1.5);
}
i can't find the Setting
Class in C#
i wanted to know if it's under a special namespace or need an additional reference to add 我无法在C#
找到Setting
类我想知道它是否在特殊命名空间下或需要额外的添加引用
If it is a custom class, and not described in the tutorial you got this from, can you not reimplement it? 如果它是一个自定义类,并且在教程中没有描述,那你可以重新实现它吗? It looks to me like the class would have a signature something like this: 在我看来,这个类会有这样的签名:
public class Setting<T>
{
public Setting<T>(string name, T value)
{
Name = name;
Value = value;
}
public string Name { get; set; }
public T Value { get; set; }
}
Of course, there could be more to it than that. 当然,可能还有更多。 What properties are being accessed / bound to on this class in the tutorial? 在本教程中,此类正在访问/绑定哪些属性?
如果您使用的是“101 Windows Phone 7 Apps”一书,则在第20章中实现并解释了Setting类 。
Here's the Setting<T>
class that I use. 这是我使用的Setting<T>
类。 It supports change notification via INotifyPropertyChanged
which is useful for binding (in WPF/SL/etc). 它支持通过INotifyPropertyChanged
进行更改通知,这对绑定很有用(在WPF / SL / etc中)。 It also maintains a copy of the default value so that it may be reset if required. 它还维护默认值的副本,以便在需要时可以重置。
As a general rule, T
should be immutable. 作为一般规则, T
应该是不可变的。
public class Setting<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public event Action<T> Changed;
private readonly T _defaultValue;
private T _value;
public Setting(T defaultValue)
{
_defaultValue = defaultValue;
_value = defaultValue;
}
public T Value
{
get { return _value; }
set
{
if (Equals(_value, value))
return;
_value = value;
var evt = Changed;
if (evt != null)
evt(value);
var evt2 = PropertyChanged;
if (evt2 != null)
evt2(this, new PropertyChangedEventArgs("Value"));
}
}
public void ResetToDefault()
{
Value = _defaultValue;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.