【问题标题】:Tap gesture with Command Parameters带有命令参数的点击手势
【发布时间】: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="&#xE74D;"
                                       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


【解决方案1】:

你可以试试这个:

在xml中:

<TapGestureRecognizer Tapped="OnDelete_Tapped" CommandParameter="{Binding .}"/>

那么 e.Parameter 将是您在 CommandParameter 中设置的任何内容。

public void OnDelete_Tapped(object sender, TappedEventArgs e)
{
    var addressType = (e.Parameter) as AddressType;
    viewModel.DeleteCommand.Execute(addressType );
    viewModel.LoadAddressTypesCommand.Execute(true);
}

或直接Using ICommand

<TapGestureRecognizer
        Command="{Binding DeleteCommand}"
        CommandParameter="{Binding .}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2013-08-03
    • 2012-12-15
    相关资源
    最近更新 更多