1

I'm using Prism.Unity.Forms with Xamarin for this project. How can I get the view to update when the Client.Id property changes? The view updates when I change the XAML from {Binding Client.Id} (a Guid object) to {Binding Client.Name}(a string).

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"
        };
    }
}

This works

<Entry Text="{Binding Client.Name}"/>

This does not

<Entry Text="{Binding Client.Id}"/>

I know the ToString method is being called on the Client.Id property because I wrapped the Guid in a custom class and overrode the ToString method, but the view still doesn't update.

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();
    }
}

1 Answers1

0

Using a Converter fixed the problem, though I can't explain why. The Guid.ToString method was being called either way.

<Entry Text="{Binding Client.Id, Converter={StaticResource GuidConverter}}"/>

public class GuidConverter : IValueConverter
{
    public object Convert()
    {
        var guid = (Guid) value;
        return guid.ToString();
    }

    public object ConvertBack(){...}
}

Then I registered the converter in App.xaml

<ResourceDictionary>
    <viewHelpers:GuidConverter x:Key="GuidConverter" />
</ResourceDictionary>