【问题标题】:Xamarin.Forms ListView ItemTapped with Corcav.Behaviors not working?Xamarin.Forms ListView ItemTapped 与 Corcav.Behaviors 不工作?
【发布时间】:2017-04-13 17:36:10
【问题描述】:

我正在尝试在corradocavalli/Corcav.Behaviors 上使用Xamarin.Forms 行为,但是我无法将ListView ItemTapped 事件绑定到ViewModel

在我的 ListView 中,我正在使用:-

<behaviors:EventToCommand  
    BindingContext="{behaviors:RelativeContext Name=MyPageName}" 
    EventName="ItemTapped"                                  
    CommandName="OnItemSelected"                               
    PassEventArgument="True" 
/>

在我的 ViewModel 中我正在使用:-

public Command<ItemTappedEventArgs> OnItemSelected { get; private set; }

构造函数:-

this.OnItemSelected = 
      new Command<ItemTappedEventArgs>(async (e) => await MyHandlerForOnItemSelected(e));

然后最后:-

private async Task MyHandlerForOnItemSelected(ItemTappedEventArgs e)
{
   ....
}

但是xaml 标记导致应用程序无法启动并挂起,因为我显然做错了什么。

有什么想法吗?

【问题讨论】:

  • 您有任何错误吗?他们说什么?
  • @GeraldVersluis 它只是挂起应用程序而不加载 xaml 页面。当我删除 xaml 中的行为标记时,应用程序会显示该页面。
  • 请检查应用程序输出窗口,可能里面有东西
  • @GeraldVersluis 输出窗口中没有任何内容。

标签: c# xamarin xamarin.forms


【解决方案1】:
<ListView ItemsSource="{Binding Items}">
          <behaviors:Interaction.Behaviors>
            <behaviors:BehaviorCollection>
              <behaviors:EventToCommand
                  BindingContext="{behaviors:RelativeContext MPage}"
                  EventName="ItemTapped"
                  CommandName="OnItemSelected"
                  PassEventArgument="True"/>
            </behaviors:BehaviorCollection>
          </behaviors:Interaction.Behaviors>
    </ListView>

此代码与您共享的代码一起工作正常,能够处理 itemtapped 事件。

【讨论】:

  • 无论出于何种原因,代码稍后才开始工作。啊,Xamarin 开发之美:-D
猜你喜欢
  • 1970-01-01
  • 2014-08-18
  • 2017-07-17
  • 2016-09-02
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多