如何将Context Menu ListView的参数传递给我的ViewModel?

时间:2016-12-09 01:26:29

标签: xaml mvvm xamarin

我必须使用选项删除来创建菜单。

我在Visual Studio Update 3中使用Xamarin。

我的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 个答案:

答案 0 :(得分:1)

您只需使用Command的{​​{3}}。

而不是:

RemoveRefeicao = new Command(() =>
{
}

它将是:

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