【发布时间】:2019-11-17 00:32:13
【问题描述】:
我有一个列表视图,其 ItemTemplate 定义如下:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.7*" />
<ColumnDefinition Width="0.3*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding AddressTypeName}" Grid.Column="0"/>
<Label Text=""
Grid.Column="1"
FontFamily="{StaticResource SegoeMDL2Assets}" FontSize="Medium" HorizontalOptions="End">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnDelete_Tapped" CommandParameter="{Binding .}"/>
</Label.GestureRecognizers>
</Label>
</Grid>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
我的文件后面的代码处理 OnDelete_Tapped 如下:
public void OnDelete_Tapped(object sender, EventArgs e)
{
viewModel.DeleteCommand.Execute(e);
viewModel.LoadAddressTypesCommand.Execute(true);
}
EventArgs e 确实返回了一个 EventArgs 对象,其中包含正确的对象(在我的例子中,是正确的 AddressType 对象)。
ViewModel,有这个定义:
public ICommand DeleteCommand => new Command<AddressType>(DeleteCommandExecute);
void DeleteCommandExecute(AddressType address)
{
if (IsBusy)
return;
IsBusy = true;
try
{
DataStore.DeleteAddressTypeAsync(address.Id);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
DeleteCommand 永远不会执行。我可以单步执行代码,但它永远不会在我的 viewModel 中调用 DeleteCommand。 LoadAddressTypesCommand 运行正常 - 不仅来自后面的代码,而且在其他地方我将该命令绑定为我的 viewModel 的命令。任何想法我做错了什么?提前致谢。
【问题讨论】:
-
您如何确定“其中包含正确对象的 EventArgs 对象”?基类
EventArgs没有任何用于包含对数据元素的引用的属性。为什么不直接绑定Command? -
可以吗?
标签: forms xamarin commandparameter