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
を使用して実行しますか?
...しかし、私はアイテムを参照するオブジェクトがパラメータとしてリストビューに触れ過ごすよう:
それは可能でしょうか? –
わかりません。すでにCommandParameter = "{Binding。}"を使用していますので、オブジェクト/パラメータがコンテキストアクションをトリガしたものです。 – Krumelur
まだ動作しません。デバッグしてもコマンドには含まれません。 –