MVVMLight's EventToCommand in Xamarin.Forms

本文关键字:in Xamarin Forms EventToCommand MVVMLight | 更新日期: 2023-09-27 18:35:30

我有Xamarin.Forms项目可移植 - 4个子项目:PCL以及iOS,WP和Droid。

我想在我的PCL项目(每个平台的共享UI)中使用Interaction.Triggers(来自System.Windows.Interactivity-确切地说来自Blend SDK)和EventToCommand(来自MVVM Light)。可能吗?

我知道 MVVM Light 是便携式的,但System.Windows.Interactivity不是,所以我有一个巨大的问题。如何在 Xamarin.Forms 中使用 EventToCommand?

MVVMLight's EventToCommand in Xamarin.Forms

在 Xamarin 项目中:

右键单击"引用","添加引用"。在程序集 -> 扩展中,可以检查和添加 System.Windows.Interactivity。

这就是它所需要的:)

我是这样做的:

首先,我将命名空间添加到我的页面

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors">

然后我向我的内容页面添加了行为

<b:Interaction.Behaviors>
  <b:BehaviorCollection>
    <b:EventToCommand EventName="Appearing" Command="{Binding InitializeViewModelCommand}" />
  </b:BehaviorCollection>
</b:Interaction.Behaviors>

希望它能有所帮助!