[英]Handling observable object properties with CommunityToolkit.Mvvm
How do we handle properties that are objects themselves using CommunityToolkit.Mvvm
?我们如何使用
CommunityToolkit.Mvvm
处理本身就是对象的属性?
I understand that I can simply use [ObservableProperty]
attribute with simple properties eg data type of string
, int
, etc.我知道我可以简单地使用具有简单属性的
[ObservableProperty]
属性,例如string
、 int
等数据类型。
How do we handle properties that are POCO
objects and need to be observable?我们如何处理
POCO
对象且需要可观察的属性?
Here's an example:这是一个例子:
public partial class MyViewModel : ObservableObject
{
[ObservableProperty]
string title;
[ObservableProperty]
decimal price;
Person author; // How do we handle this property that is a Person object?
}
I understand that source generators in CommunityToolkit.Mvvm
will automatically handle creation of Title
and Price
public properties.我了解
CommunityToolkit.Mvvm
中的源生成器将自动处理Title
和Price
公共属性的创建。 What about author
? author
呢?
Do we use [ObservableProperty]
attribute to make complex properties observable, such as the author
property in the above example which is a Person
object?我们是否使用
[ObservableProperty]
属性来使复杂的属性可观察,例如上面示例中的author
属性是一个Person
对象?
Here are multiple scenarios:这里有多个场景:
The entire Person object gets replaced整个 Person 对象被替换
If you only ever replace the entire Person object and don't change its individual properties (you might select a different person from a list) all you need is to decorate the author
field with the ObservablePropertyAttribute
.如果您只替换整个 Person 对象并且不更改其各个属性(您可能会从列表中选择不同的人),那么您只需要使用
ObservablePropertyAttribute
装饰author
字段。
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class MyViewModel : ObservableObject
{
[ObservableProperty]
string title;
[ObservableProperty]
decimal price;
[ObservableProperty]
Person author;
}
This change will be reflected in the UI:此更改将反映在 UI 中:
Author = anotherPerson;
and this one will NOT:这个不会:
Author.FirstName = "James";
Individial Person properties get changed个人 Person 属性已更改
If you plan to change the persons individial properties, but keep the instance the same, you need to decorate the individual properties of the Person
object with the ObservablePropertyAttribute
.如果您打算更改 Person 的各个属性,但保持实例相同,则需要使用
ObservablePropertyAttribute
装饰Person
对象的各个属性。
public class Person : ObservableObject
{
[ObservableProperty]
string firstName;
[ObservableProperty]
string lastName;
}
public partial class MyViewModel : ObservableObject
{
[ObservableProperty]
string title;
[ObservableProperty]
decimal price;
public Person Author { get; } = new Person();
}
This change will be reflected in the UI:此更改将反映在 UI 中:
Author.FirstName = "James";
and this one will NOT:这个不会:
Author = anotherPerson;
Or both或两者
Or do both and get the UI to reflect any changes, be it changing an individual property or the entire object instance.或者两者都做,让 UI 反映任何更改,无论是更改单个属性还是整个对象实例。
public class Person : ObservableObject
{
[ObservableProperty]
string firstName;
[ObservableProperty]
string lastName;
}
public partial class MyViewModel : ObservableObject
{
[ObservableProperty]
string title;
[ObservableProperty]
decimal price;
[ObservableProperty]
Person author;
}
Any of these changes will be reflected in the UI:这些更改中的任何一个都将反映在 UI 中:
Author = anotherPerson;
Author.FirstName = "James";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.