2017-06-29 2 views
1

コマンドにパラメータを渡そうとしています。ボタンがパラメータでコマンドを起動しない

私はXAMLコードに次き:

<Button Text="{Binding ButtonText}" x:Name="btnCaptureNegotiation" BackgroundColor="#3276b1" 
         TextColor="White" Clicked="OnCaptureNegotiationClicked" 
         CommandParameter="{Binding Client, Path=cod_cte}" Command="{Binding LoadULastNegotiationCommand}" ></Button> 
        <StackLayout Orientation="Vertical" x:Name="captureLayout" IsVisible="{Binding IsVisible}"> 
<!-- more code --> 

そして、私はこのようにバインドさコードビハインドに:私はことを発見

public class NegotiationVM : INotifyPropertyChanged 
    { 
     private bool _isVisible = false; 
     private string _buttonText = "Capturar Seguimiento"; 

     private Client _client; 
     public Client Client{ 
      get { return _client; } 
      set { 
       if (this._client != value) 
        _client = value; 
       NotifyPropertyChanged("Client"); 
      } 
     } 

     private Models.NegotiationRepository _negotiationRepo; 

     public ICommand LoadULastNegotiationCommand { get; private set; } 
     public int LoadLasNegotiationResult { get; private set; } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public NegotiationVM(){ 

      LoadULastNegotiationCommand = new Command<string (LoadLastNegotiationAsync); 
     } 


     private void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     async void LoadLastNegotiationAsync(string value) 
     { 
      _negotiationRepo = new Models.NegotiationRepository(); 
      LoadLasNegotiationResult = await _negotiationRepo.GetLastNegotiationActiveAsync(value); 
      NotifyPropertyChanged("LoadLastNegotiationAsync"); 
     } 

     public bool IsVisible 
     { 
      get 
      { 
       return _isVisible; 
      } 
      set 
      { 
       if (this._isVisible != value) 
        _isVisible = value; 
       if (this._isVisible){ 
        this.ButtonText = "Cancel"; 
       }else{ 
        this.ButtonText = "Capture Negotation"; 
       } 
       NotifyPropertyChanged("IsVisible"); 
      } 
     } 

     public string ButtonText { 
      get 
      { 
       return _buttonText; 
      } 
      set 
      { 
       if (this._buttonText != value) 
        _buttonText = value; 
       NotifyPropertyChanged("ButtonText"); 
      } 
     } 
} 

public Client client; 
public NegociationVM negotiation = new NegotiationVM(); 

public ClientItemPage(Client client) 
     { 
      this.client = client; 
      negotiation.Client = client; //STOP WORKING after adding this line 
      InitializeComponent(); 
      captureLayout.BindingContext = negotiation; 
      btnCaptureNegotiation.BindingContext = negotiation; 
     } 

private void OnCaptureNegotiationClicked(object sender, EventArgs args) 
     { 
      negotiation.IsVisible = !negotiation.IsVisible; 
     } 

... 

そしてNegotiationVMクラスコマンドは起動され、サービスからリソースを取得しようとしますが、私は404を取得します。これは、パラメータが送信されていないことがわかったためです私はちょうどそれを見つけるために、非同期void LoadLastNegotiationAsync(文字列値)メソッドにブレークポイントを置く。

パブリックコンストラクタでコードビハインドページで何も送信していないため、私は、ネゴシエーションで同じ名前のプロパティにClientを設定しました(NegotiationVMのインスタンス)。コメントが示唆しているように、コマンドは停止し、ボタンを押すだけでその行が追加されます。

このバインディングで何が問題になっていますか?そのクライアントの文字列プロパティを正しく送信するにはどうすればよいですか?

答えて

1

Clienteのプロパティがcod_cteの場合。プロパティがClientではなくCliente命名されている場合は、

CommandParameter="{Binding Cliente.cod_cte}" 

Clienteに末尾eを省略:そうのように結合し

CommandParameter="{Binding Client.cod_cte}"