2016-12-09 1 views
0

削除するオプションを含むメニューを作成する必要があります。私は、Visual StudioのアップデートでXamarinを使用していコンテキストメニューListViewのパラメータをViewModelに渡すにはどうすればよいですか?

3.

私のXAML

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="Refeicao.View.ListaRefeicaoView" 
      Title="Lista de Refeições"> 
    <ContentPage.Content> 
    <ListView ItemsSource="{Binding Refeicoes}"> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <ViewCell.ContextActions> 
       <MenuItem Text="Apagar" Command="{Binding RemoveRefeicao}" CommandParameter="{Binding .}"/> 
       </ViewCell.ContextActions> 
      <StackLayout> 
       <Label Text="{Binding Descricao, Mode=TwoWay}"/> 
       <StackLayout> 
       <Label Text="Calorias: " /> 
       <Label Text="{Binding Calorias}" /> 
       </StackLayout> 
      </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
    </ContentPage.Content> 
</ContentPage> 

マイXAML.cs

namespace Refeicao.View 
{ 
    public partial class ListaRefeicaoView : ContentPage 
    { 
     public ListaRefeicaoView(RefeicaoDAO refeicaoDao) 
     { 
      CadastroRefeicaoViewModel cadastroRefeicaoViewModel = new CadastroRefeicaoViewModel(refeicaoDao, this); 
      BindingContext = cadastroRefeicaoViewModel; 
      InitializeComponent(); 
     } 
    } 
} 

そして、私のViewModelクラス

namespace Refeicao.ViewModel 
{ 
    public class CadastroRefeicaoViewModel : INotifyPropertyChanged 
    { 
     private RefeicaoDAO _refeicaoDao; 
     private ContentPage _contentPage; 
     public event PropertyChangedEventHandler PropertyChanged; 
     private string _descricao; 
     private double _calorias; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public string Descricao 
     { 
      get { return _descricao; } 
      set 
      { 
       if (Equals(_descricao, value)) return; 
       _descricao = value; 
       OnPropertyChanged(nameof(Descricao)); 
      } 
     } 

     public double Calorias 
     { 
      get { return _calorias; } 
      set 
      { 
       if(Equals(_calorias, value)) return; 
       _calorias = value; 
       OnPropertyChanged(nameof(Calorias)); 
      } 
     } 

     public ObservableCollection<RefeicaoModel> Refeicoes { get; set; } 

     public Command SalvaRefeicao { get; private set; } 
     public Command RemoveRefeicao { get; private set; } 

     public CadastroRefeicaoViewModel(RefeicaoDAO refeicaoDao, ContentPage contentPage) 
     { 
      _refeicaoDao = refeicaoDao; 
      _contentPage = contentPage; 

      Refeicoes = _refeicaoDao.Refeicoes; 

      SalvaRefeicao = new Command(() => 
      { 
       RefeicaoModel refeicao = new RefeicaoModel() { Descricao = _descricao, Caloria = _calorias }; 
       _refeicaoDao.Salvar(refeicao); 

       string msg = $"A refeição {_descricao} de {_calorias} foi salva."; 
       _contentPage.DisplayAlert("Refeição Salva", msg, "OK"); 
      }); 

      RemoveRefeicao = new Command(() => 
      { 
       RefeicaoModel refeicao = new RefeicaoModel() { Descricao = _descricao, Caloria = _calorias }; 
       _refeicaoDao.Remover(refeicao); 

       string msg = $"A refeição {_descricao} foi removida."; 
       _contentPage.DisplayAlert("Refeição Removida", msg, "OK"); 
      }); 
     } 
    } 
} 

また、どのように行うかコードの背後には何もありませんか? このコマンドを単にTappedを使用して実行しますか?

答えて

1

Commandにはcorrect constructorを使用してください。

の代わりに:理解

RemoveRefeicao = new Command(p => 
{ 
    var yourData = (RefeicaoModel)p; 
} 
+0

...しかし、私はアイテムを参照するオブジェクトがパラメータとしてリストビューに触れ過ごすよう:

RemoveRefeicao = new Command(() => { } 

それは可能でしょうか? –

+0

わかりません。すでにCommandParameter = "{Binding。}"を使用していますので、オブジェクト/パラメータがコンテキストアクションをトリガしたものです。 – Krumelur

+1

まだ動作しません。デバッグしてもコマンドには含まれません。 –

関連する問題