简体   繁体   中英

What is the difference between an AutoProperty and Member

In MugenMvvmToolkit , What is the difference when using CreateAutoProperty vs using CreateMember . What is an example of when you'd use CreateAutoProperty vs CreateMember ?

CreateAutoProperty is analog of auto property from C#, it creates get, set and observe members, you can listen when this property is changed.

CreateMember you can control logic of set, get and observe methods you are responsible where you will store the state of this member.

public class AttachedPropertySample
{
    private string _autoProperty;
​
    //this is equivalent to AttachedBindingMember.CreateAutoProperty<AttachedPropertySample, string>("AutoProperty", OnAutoPropetyChanged);
    //In auto member you cannot control logic of set and get method you can also listen when this property is changed
    public string AutoProperty
    {
        get { return _autoProperty; }
        set
        {
            _autoProperty = value;
            OnAutoPropetyChanged();
        }
    }

    //this is equivalent to ​AttachedBindingMember.CreateMember<AttachedPropertySample, string>("CustomMember", (info, sample) => "My custom value", (info, sample, arg3) => CustomMemberSetter(arg3))
    //In custom member you can control logic of set, get and observe methods
    public string CustomMember
    {
        get { return "My custom value"; }
        set { CustomMemberSetter(value); }
    }

    private void CustomMemberSetter(string value)
    {

    }
​
    private void OnAutoPropetyChanged()
    {
    }
}

You can use CreateAutoProperty when you need to store some value and react when this value is changed.

Here's an example when I used CreateAutoProperty :

AttachedBindingMember.CreateAutoProperty<ActionBar, bool>("DisplayShowHomeEnabled", (actionBar, args) => actionBar.SetDisplayShowHomeEnabled(args.NewValue));

ActionBar doesn't have the DisplayShowHomeEnabled property and I added it using CreateAutoProperty .

Here's an example when I used CreateMember :

AttachedBindingMember.CreateMember<IMenuItem, bool>(nameof(IMenuItem.IsVisible), (info, item) => item.IsVisible, (info, item, value) => item.SetVisible(value));

IMenuItem already has the IsVisible getter and the SetVisible method I combined them using the CreateMember .

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