简体   繁体   中英

“Setting” class in C#

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

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

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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