편집

다음을 통해 공유


Microsoft.Toolkit.Mvvm.ComponentModel Namespace

Classes

AlsoNotifyChangeForAttribute

An attribute that can be used to support ObservablePropertyAttribute in generated properties. When this attribute is used, the generated property setter will also call OnPropertyChanged(String) (or the equivalent method in the target class) for the properties specified in the attribute data. This can be useful to keep the code compact when there are one or more dependent properties that should also be reported as updated when the value of the annotated observable property is changed. If this attribute is used in a field without ObservablePropertyAttribute, it is ignored.

In order to use this attribute, the containing type has to implement the INotifyPropertyChanged interface and expose a method with the same signature as OnPropertyChanged(String). If the containing type also implements the INotifyPropertyChanging interface and exposes a method with the same signature as OnPropertyChanging(String), then this method will be invoked as well by the property setter.

This attribute can be used as follows:

partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    [AlsoNotifyChangeFor(nameof(FullName))]
    private string name;

    [ObservableProperty]
    [AlsoNotifyChangeFor(nameof(FullName))]
    private string surname;

    public string FullName => $"{Name} {Surname}";
}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public string Name
    {
        get => name;
        set
        {
            if (SetProperty(ref name, value))
            {
                OnPropertyChanged(nameof(FullName));
            }
        }
    }

    public string Surname
    {
        get => surname;
        set
        {
            if (SetProperty(ref surname, value))
            {
                OnPropertyChanged(nameof(FullName));
            }
        }
    }
}
INotifyPropertyChangedAttribute

An attribute that indicates that a given type should implement the INotifyPropertyChanged interface and have minimal built-in functionality to support it. This includes exposing the necessary event and having two methods to raise it that mirror OnPropertyChanged(PropertyChangedEventArgs) and OnPropertyChanged(String). For more extensive support, use ObservableObjectAttribute.

This attribute can be used as follows:

[INotifyPropertyChanged]
partial class MyViewModel : SomeOtherClass
{
    // Other members here...
}

ObservableObject

A base class for objects of which the properties must be observable.

ObservableObject.TaskNotifier

A wrapping class that can hold a Task value.

ObservableObject.TaskNotifier<T>

A wrapping class that can hold a Task<TResult> value.

ObservableObjectAttribute

An attribute that indicates that a given type should have all the members from ObservableObject generated into it, as well as the INotifyPropertyChanged and INotifyPropertyChanging interfaces. This can be useful when you want the same functionality from ObservableObject into a class that already inherits from another one (since C# doesn't support multiple inheritance). This attribute will trigger the source generator to just create the same APIs directly into the decorated class.

This attribute can be used as follows:

[ObservableObject]
partial class MyViewModel : SomeOtherClass
{
    // Other members here...
}

And with this, the same APIs from ObservableObject will be available on this type as well.
ObservablePropertyAttribute

An attribute that indicates that a given field should be wrapped by a generated observable property. In order to use this attribute, the containing type has to implement the INotifyPropertyChanged interface and expose a method with the same signature as OnPropertyChanged(String). If the containing type also implements the INotifyPropertyChanging interface and exposes a method with the same signature as OnPropertyChanging(String), then this method will be invoked as well by the property setter.

This attribute can be used as follows:

partial class MyViewModel : ObservableObject
{
    [ObservableProperty]
    private string name;

    [ObservableProperty]
    private bool isEnabled;
}

And with this, code analogous to this will be generated:
partial class MyViewModel
{
    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }

    public bool IsEnabled
    {
        get => name;
        set => SetProperty(ref isEnabled, value);
    }
}
ObservableRecipient

A base class for observable objects that also acts as recipients for messages. This class is an extension of ObservableObject which also provides built-in support to use the IMessenger type.

ObservableRecipientAttribute

An attribute that indicates that a given type should have all the members from ObservableRecipient generated into it. This can be useful when you want the same functionality from ObservableRecipient into a class that already inherits from another one (since C# doesn't support multiple inheritance). This attribute will trigger the source generator to just create the same APIs directly into the decorated class. For instance, this attribute can be used to easily combine the functionality from both ObservableValidator and ObservableRecipient, by using ObservableValidator as the base class and adding this attribute to the declared type.

This attribute can be used as follows:

[ObservableRecipient]
partial class MyViewModel : ObservableValidator
{
    // Other members here...
}

And with this, the same APIs from ObservableRecipient will be available on this type as well.

To avoid conflicts with other APIs in types where the new members are being generated, constructors are only generated when the annotated type doesn't have any explicit constructors being declared. If that is the case, the same constructors from ObservableRecipient are emitted, with the accessibility adapted to that of the annotated type. Otherwise, they are skipped, so the type being annotated has the respondibility of properly initializing the Messenger property. Additionally, if the annotated type inherits from ObservableValidator, the SetProperty<T>(T, T, Boolean, String) overloads will be skipped as well, as they would conflict with the SetProperty<T>(T, T, Boolean, String) methods.

ObservableValidator

A base class for objects implementing the INotifyDataErrorInfo interface. This class also inherits from ObservableObject, so it can be used for observable items too.