2017-07-12 4 views
1

このプロジェクトでは、XamarinでPrism.Unity.Formsを使用しています。 Client.Idプロパティが変更されたときに、ビューを更新するにはどうすればよいですか? {Binding Client.Id}(Guidオブジェクト)から{Binding Client.Name}(文字列)にXAMLを変更すると、ビューが更新されます。Xamarinフォームビューがカスタムクラスで更新されない

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

これは私がカスタムクラスでGuidを包み、オーバーライドのでToString方法がClient.Idプロパティに呼び出されている知っている

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

しません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(); 
    } 
} 
+0

App.xamlにコンバータを登録しました –

答えて

0

Converterを使用すると、理由を説明できませんが、問題を解決しました。 Guid.ToStringメソッドはいずれかの方法で呼び出されていました。

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

    public object ConvertBack(){...} 
} 

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

は、その後、私はあなたのクライアントがINotifyPropertyChangedの実装を作るために必ず

<ResourceDictionary> 
    <viewHelpers:GuidConverter x:Key="GuidConverter" /> 
</ResourceDictionary> 
関連する問題