C# 如何从Xamrin.Form中的ListView和ViewModelObserverAgeCollection中删除项?
我想从Xamrin.Form中的ListView和ViewModelObserverAgeCollection中删除项 雇员结果C# 如何从Xamrin.Form中的ListView和ViewModelObserverAgeCollection中删除项?,c#,listview,xamarin.forms,mvvm-light,C#,Listview,Xamarin.forms,Mvvm Light,我想从Xamrin.Form中的ListView和ViewModelObserverAgeCollection中删除项 雇员结果 <ListView x:Name="EmployeeResultsListView" ItemsSource="{Binding EmployeeResults}" RowHeight="200" IsPullToRefreshEnabled="true" RefreshComman
<ListView x:Name="EmployeeResultsListView"
ItemsSource="{Binding EmployeeResults}"
RowHeight="200"
IsPullToRefreshEnabled="true"
RefreshCommand="{Binding RefreshDataCommand}"
IsRefreshing="{Binding IsRefreshingData, Mode=OneWay}"
ItemAppearing="Employee_ItemAppearing"
<ListView.ItemTemplate>
<DataTemplate>
<local:EmployeeResultViewCell />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
更改:删除单击事件并添加绑定。对于MVVM,您不使用事件,而是使用viewmodels中的命令,您可以将这些命令绑定到视图的命令。要实现您想要的功能,最有效的MVVM方法是在单元格中添加一个
ICommand
bindable属性
public partial class EmployeeResultViewCell : CustomViewCell
{
/// <summary>
/// The <see cref="DeleteCommand" /> bindable property.
/// </summary>
public static readonly BindableProperty DeleteCommandProperty = BindableProperty.Create(nameof(SealCheckListPage.DeleteCommand), typeof(ICommand), typeof(SealCheckListPage), default(ICommand));
/// <summary>
/// Gets or sets the DeleteCommand that is called when we'd like to delete an employee.
/// </summary>
public ICommand DeleteCommand
{
get => (ICommand)this.GetValue(SealCheckListPage.DeleteCommandProperty);
set => this.SetValue(SealCheckListPage.DeleteCommandProperty, value);
}
private void MenuItemDelete_Clicked(object sender, System.EventArgs e)
{
DeleteCommand?.Execute(BindingContext);
}
}
请注意,包含列表视图的页面必须具有x:Name=“page”
,才能正确绑定命令。诚然,以这种方式绑定命令并不是最理想的,但据我所知,这是我们所能做的最好的,MVVM方面的
最后一件事是将ICommand
属性添加到viewmodel中
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<local:EmployeeResultViewCell DeleteCommand="{Binding BindingContext.DeleteCommand, Source={x:Reference Page}}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public class EmployeeResultsViewModel : ViewModelBase
{
private async Task LoadEmployee()
{
EmployeeResults = GetDataUsingAPI(); //15 records per call
DeleteCommand = new Command<ExtendedEmployee>(OnDelete);
}
public ICommand DeleteCommand { get; }
private void OnDelete(ExtendedEmployee employee)
{
// delete the employee from the collection
}
/* ... */
}
public类EmployeeResultsViewModel:ViewModelBase
{
私有异步任务LoadEmployee()
{
EmployeeResults=GetDataUsingAPI();//每次调用15条记录
DeleteCommand=新命令(OnDelete);
}
公共ICommand DeleteCommand{get;}
私有void OnDelete(扩展雇员)
{
//从集合中删除该员工
}
/* ... */
}
现在,当您的单元格接收到事件时,它将执行命令,该命令绑定到viewmodel中的命令。执行此命令时,它将执行传递的委托OnDelete
,在该委托中,您可以访问要从中删除ExtendedEmployee
的集合。您好,非常感谢。我已经在菜单项中编辑了我的问题和设置命令。但我仍然无法调用ListPage的ViewModel命令。你能纠正我吗。我认为这行菜单项中有问题Command=“{Binding Path=BindingContext.OnDeleteEmployeeCommand,Source={x:Reference EmployeeResultsPage}}”
@NanjiMange您检查过调试输出了吗?如果绑定失败,应该有一条调试消息告诉您失败的原因。此外,通过在此处设置断点来检查MenuItemDelete\u是否单击了
。您是否为DeleteCommand
创建了属性和BindableProperty
(或者根据您的喜好简单地创建Command
),它工作正常。非常感谢你。你救了我一天。
public partial class EmployeeResultViewCell : CustomViewCell
{
/// <summary>
/// The <see cref="DeleteCommand" /> bindable property.
/// </summary>
public static readonly BindableProperty DeleteCommandProperty = BindableProperty.Create(nameof(SealCheckListPage.DeleteCommand), typeof(ICommand), typeof(SealCheckListPage), default(ICommand));
/// <summary>
/// Gets or sets the DeleteCommand that is called when we'd like to delete an employee.
/// </summary>
public ICommand DeleteCommand
{
get => (ICommand)this.GetValue(SealCheckListPage.DeleteCommandProperty);
set => this.SetValue(SealCheckListPage.DeleteCommandProperty, value);
}
private void MenuItemDelete_Clicked(object sender, System.EventArgs e)
{
DeleteCommand?.Execute(BindingContext);
}
}
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<local:EmployeeResultViewCell DeleteCommand="{Binding BindingContext.DeleteCommand, Source={x:Reference Page}}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
public class EmployeeResultsViewModel : ViewModelBase
{
private async Task LoadEmployee()
{
EmployeeResults = GetDataUsingAPI(); //15 records per call
DeleteCommand = new Command<ExtendedEmployee>(OnDelete);
}
public ICommand DeleteCommand { get; }
private void OnDelete(ExtendedEmployee employee)
{
// delete the employee from the collection
}
/* ... */
}