Xamarin表单将单击的项目传递给命令作为命令参数 [英] Xamarin forms pass clicked item to command as command parameter

查看:49
本文介绍了Xamarin表单将单击的项目传递给命令作为命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Xamarin表单,现在有了自定义模板中显示的项目列表.

I just started with Xamarin forms an now I have a list of items that I display in a custom template.

我想要的行为是在页面上下文上触发事件(使用 Corcav.Behaviors ),但我想将单击的项目传递给命令.我似乎无法完成最后一部分的工作.通过以下实现,事件可以正确触发,但是传递的参数是MyEventsListModel,但是我希望单击该项目.

The behavior that i want is that the event fired on the context of the page (using Corcav.Behaviors), but I want to pass the clicked item to the command. I can't seem to get the last part to work. With the below implementation the event fires correctly but the passed parameter is the MyEventsListModel but I want the item that is clicked.

请注意,我最好要在xaml/viewmodel中而不是在后面的代码中提供解决方案.另外,两个事件都在该事件上触发的替代解决方案也很好.

note I preferably am want a solution in xaml/viewmodel and not in the code-behind. And alternate solution where both events fire on the Event is also fine.

Xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behaviors="clr-namespace:Corcav.Behaviors;assembly=Corcav.Behaviors"
             x:Class="TheEventsApp.Mobile.MyEventsList"
             Title="My Events"
             x:Name="MainPage">
    <ListView ItemsSource="{Binding Events}">
        <behaviors:Interaction.Behaviors>
            <behaviors:BehaviorCollection>
                <behaviors:EventToCommand EventName="ItemTapped" Command="{Binding NavigateToEventDetails}" CommandParameter="{Binding .}" />
            </behaviors:BehaviorCollection>
        </behaviors:Interaction.Behaviors>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding Name}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

Viewmodel:

Viewmodel:

 [ImplementPropertyChanged]
 public class MyEventsListModel : FreshBasePageModel
 { 
    private readonly EventsDatastore eventsStore;

    public MyEventsListModel(EventsDatastore eventsStore)
    {
        this.eventsStore = eventsStore;
    }

    protected async override void ViewIsAppearing(object sender, EventArgs e)
    {
        this.Events = await eventsStore.GetMyEventsAsync();
    }

    public ObservableCollection<Event> Events { get; set; } = new ObservableCollection<Event>();

    public Command NavigateToEventDetails
    {
        get
        {
            return new Command(async (clickedEvent) =>
            {
                await CoreMethods.PushPageModel<EventDetailsPageModel>(clickedEvent);
            });
        }
    }
}

推荐答案

解决方案非常简单.深入研究Corcav的源代码后,就很容易找到答案. EventToCommand.cs

The solution was fairly simple. After diving into the sourcecode of Corcav it was easy enough to figure it out. The following code in EventToCommand.cs

private void OnFired(EventArgs e)
{
    object param = this.PassEventArgument ? e : this.CommandParameter;

    if (!string.IsNullOrEmpty(this.CommandName))
    {
        if (this.Command == null) this.CreateRelativeBinding();
    }

    if (this.Command == null) throw new InvalidOperationException("No command available, Is Command properly set up?");

    if (e == null && this.CommandParameter == null) throw new InvalidOperationException("You need a CommandParameter");

    if (this.Command != null && this.Command.CanExecute(param))
    {
        this.Command.Execute(param);
    }
}

处理它.只需将"PassEventArgument"设置为true即可为我解决问题.

Handles it. Simply setting "PassEventArgument" to true solved the issue for me.

<behaviors:EventToCommand EventName="ItemTapped" Command="{Binding NavigateToEventDetails}" PassEventArgument="True" />

这篇关于Xamarin表单将单击的项目传递给命令作为命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆