0

我不知道如何通过 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();
    }
}

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

4

2 回答 2

0

我认为你不需要使用CommandParameter,你可以CategoryListItem.Id直接在你的ItemTappedEventArgsConverter

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
}
于 2019-08-22T08:44:35.013 回答
0

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

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

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

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

请参阅此处的文档: https ://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html

于 2019-08-24T01:00:02.573 回答