【问题标题】:Xamarin Forms Prism Pass a property via CommandParameterXamarin Forms Prism 通过 CommandParameter 传递属性
【发布时间】:2019-08-21 13:51:09
【问题描述】:

我不知道如何通过 CommandParameter 将单个属性传递给 ViewModel 中的 Command 函数。

现在我传递了整个对象 (CategoryListItem),但我只需要来自 Id 属性的值。我怎样才能做到这一点?

类:

public class CategoryListItem
{
    public int Id { get; set; }
    public int Order { get; set; }

    public string Name { get; set; }
}

视图模型:

public ObservableRangeCollection<CategoryListItem> Listing
    {
        get => _listing;
        set => SetProperty(ref _listing, value);
    }

OnItemTappedCommand = new DelegateCommand<CategoryListItem>(OnItemTapped);

private async void OnItemTapped(CategoryListItem obj)
    {
        await _navigationService.GoBackAsync(new NavigationParameters
        {
            { "CategoryId", obj.Id }
        });
    }

XAML:

<ListView
        ItemsSource="{ Binding Listing }"
        HasUnevenRows="false"
        CachingStrategy="RecycleElement"
        SeparatorColor="{ DynamicResource AccentColor }">

        <ListView.Behaviors>
            <b:EventToCommandBehavior 
                EventName="ItemTapped"
                Command="{ Binding OnItemTappedCommand }"
                CommandParameter="???"
                EventArgsConverter="{ StaticResource ItemTappedEventArgsConverter }">
            </b:EventToCommandBehavior>
        </ListView.Behaviors>

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label 
                        Text="{ Binding Name }"
                        VerticalTextAlignment="Center"
                        HorizontalTextAlignment="Center"
                        FontSize="16" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>

转换器:

public class ItemTappedEventArgsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is ItemTappedEventArgs itemTappedEventArgs))
        {
            throw new ArgumentException("error");
        }

        return itemTappedEventArgs.Item;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

欢迎任何想法!没有什么对我有用。

【问题讨论】:

  • 现在可以用了吗?

标签: mvvm xamarin.forms prism delegatecommand


【解决方案1】:

我认为您不需要使用CommandParameter,您可以直接在ItemTappedEventArgsConverter 中返回CategoryListItem.Id

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (!(value is ItemTappedEventArgs itemTappedEventArgs))
    {
        throw new ArgumentException("error");
    }

    return ((CategoryListItem)itemTappedEventArgs.Item).Id;
}

然后在你ViewModel

OnItemTappedCommand = new DelegateCommand<int>(OnItemTapped);

private async void OnItemTapped(int id)
{
  // here you could get the Id property
}

【讨论】:

  • 好主意,但转换器是混淆的。只需将点击的项目传递给命令,它就可以获得自己的 id 或任何需要的东西。
  • @Haukinger 是的,所以我觉得你的需求有点奇怪,你可以直接获取 CategoryListItem 数据,如果你想要它的 Id,只需要像 obj.Id 一样,你也可以得到名字比如 obj.Name 等等..
  • @LeoZhu-MSFT 我知道,但是当我不需要整个对象时只传递 Id 会不会更好?
  • @LeoZhu-MSFT 我不关心性能(现在),而是关心可读性。将不必要的转换器放入绑定中会使代码更难理解。
  • @NanouPonette 我认为在你的情况下它可以忽略不计,但无论如何,你优化性能的想法是好的。
【解决方案2】:

我认为你不能从那里绑定到被窃听的记录。您也许可以在 ItemTemplate 中执行某些操作,例如在视图单元格上创建行为并从那里传递属性。

如果您想避免使用转换器,您可以改用“EventArgsParameterPath”:

           <b:EventToCommandBehavior 
            EventName="ItemTapped"
            Command="{Binding OnItemTappedCommand}"
            EventArgsParameterPath="Item">
        </b:EventToCommandBehavior>

将其设置为“Item”,它仍然会发送整个 CategoryListItem。

在此处查看文档: https://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html

【讨论】:

  • 发送整个 CategoryListItem 有效,但我想要一个只发送对象 ID 的通用解决方案。
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 2021-12-23
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
相关资源
最近更新 更多