简体   繁体   中英

c# class written in simple terms for wpf mvvm

I've come across an example tutorial online and saw a property for a class written like this for a wpf project. I was wondering what this would translate to in simpler terms since i've never seen a class written like this before.

Is the translation i attempted correct? Meaning is it doing the same thing the tutorial class is doing?

Tutorial Class

public ObservableCollection<string> Directories
{
    get { return Get<ObservableCollection<string>>(); }
    set { Set(value); }
}

My attempted guess at what it would be written as

private ObservableCollection<string> _directories; 
public ObservableCollection<string> Directories
{
    get
    {
        return this._directories;
    }

    set
    {
        this._directories= value;
    }
}

We'd have to see the Get/Set implementations to answer this question properly but my guess is it's been done to improve code readability. There are two things you often see in MVVM properties: auto-initialization in the getter and property change notification in the setter...

    private ObservableCollection<string> _directories;
    public ObservableCollection<string> Directories
    {
        get
        {
            // auto-initialize
            if (this._directories == null)
                this._directories = new ObservableCollection<string>();
            return this._directories;
        }
        set
        {
            this._directories = value;
            RaisePropertyChanged("Directories"); // INPC
        }
    }

This leads to a lot of code bloat if you're doing it for a large number of properties. My guess is the Set and Get methods in your tutorial class are probably being used to implement these features automatically, or at least something similar. The following code would provide that functionality for properties such as the one you posted:

    private Dictionary<string, object> Properties = new Dictionary<string, object>();

    public T Get<T>([CallerMemberName] string propertyName = null)
    {
        if (!Properties.ContainsKey(propertyName))
            Properties[propertyName] = default(T);
        return (T)Properties[propertyName];
    }

    public void Set<T>(T value, [CallerMemberName] string propertyName = null)
    {
        Properties[propertyName] = value;
        RaisePropertyChanged(propertyName);
    }

Short answer: No it is not doing the same thing.

The tutorial class depicts that there should be two additional methods like this:

 private void Set(ObservableCollection<string> value)
 {
       //  do something
 }

 private T Get<T>()
 {
       //  do something
 }

The following example is the auto property version of your attempted guess:

public ObservableCollection<string> Directories
{
      get; set;
}

Maybe that is what the tutorial is trying to show?

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