【问题标题】:Xamarin forms pass clicked item to command as command parameterXamarin 表单将单击的项目作为命令参数传递给命令
【发布时间】:2017-02-24 22:00:58
【问题描述】:

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

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

请注意,我最好希望在 xaml/viewmodel 中而不是在代码隐藏中找到解决方案。两个事件都在 Event 上触发的替代解决方案也很好。

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>

视图模型:

 [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);
            });
        }
    }
}

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    解决方案相当简单。在深入研究 Corcav 的源代码之后,很容易弄清楚。以下代码在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 即可解决我的问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2019-07-13
      • 2014-10-25
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多