"设置"在C#类 [英] "Setting" class in C#

查看:118
本文介绍了"设置"在C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和Sliverlight的阅读教程为Windows Phone 7,我发现这行

i was reading a tutorial for windows phone 7 using C# and sliverlight and i found this line

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

我无法找到设置类的<​​code> C#我想知道,如果它是在一个特殊的命名空间或需要一个额外的引用添加

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

推荐答案

这里的设置&LT; T&GT; 类,我使用。它支持通过 INotifyPropertyChanged的这是(在WPF / SL /等)结合有用的更改通知。它也保持默认值的副本,这样如果需要的话它可能会被重置。

Here's the Setting<T> class that I use. It supports change notification via INotifyPropertyChanged which is useful for binding (in WPF/SL/etc). It also maintains a copy of the default value so that it may be reset if required.

作为一般规则, T 应该是一成不变的。

As a general rule, T should be immutable.

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

这篇关于&QUOT;设置&QUOT;在C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆