我在这个项目中使用 Prism.Unity.Forms 和 Xamarin。Client.Id
属性更改时如何更新视图?当我将 XAML 从{Binding Client.Id}
(一个 Guid 对象)更改为{Binding Client.Name}
(一个字符串)时,视图会更新。
public class CreateClientViewModel : BindableBase
{
private Client _client;
public Client Client {
get => _client;
set => SetProperty(ref _client, value);
}
private async void FetchNewClient()
{
Client = new Client{
Id = new Guid.Parse("501f1302-3a45-4138-bdb7-05c01cd9fe71"),
Name = "MyClientName"
};
}
}
这有效
<Entry Text="{Binding Client.Name}"/>
这不
<Entry Text="{Binding Client.Id}"/>
我知道该ToString
方法正在Client.Id
属性上被调用,因为我将它包装Guid
在一个自定义类中并覆盖了该ToString
方法,但视图仍然没有更新。
public class CreateClientViewModel : BindableBase
{
private Client _client;
public Client Client {
get => _client;
set => SetProperty(ref _client, value);
}
//This method will eventually make an API call.
private async void FetchNewClient()
{
Client = new Client{
Id = new ClientId{
Id = new Guid.Parse("501f1302-3a45-4138-bdb7-05c01cd9fe71")
},
Name = "MyClientName"
};
}
}
public class ClientId
{
public Guid Id { get; set }
public override string ToString()
{
//This method gets called
Console.WriteLine("I GET CALLED");
return Id.ToString();
}
}