Xamarin 窗体 EventToCommandBehavior

Kul*_*gar 0 c# xamarin xamarin.forms

我一直在尝试遵循位于以下位置的 EventToCommandBehavior 示例:

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/behaviors/reusable/event-to-command-behavior/

该示例显示了为列表视图中的项目触发的 selectedItem 事件。我希望为列表视图中的任何项目触发 Switch 事件。

我确实确保将 EventName 从“ItemsSelected”更改为“Toggled”(切换事件),但根本没有触发该事件。我可能做错了什么?

FWIW,我正在尝试遵循 MVVM 模式并最小化“代码隐藏”

车库页面.xaml

  <ContentPage.Resources>
    <ResourceDictionary>
        <converters:ToggledItemEventArgsConverter x:Key="ToggledConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

                  <ListView x:Name="listView" VerticalOptions="StartAndExpand" HasUnevenRows="true" ItemsSource="{Binding Previews}" HeightRequest="800" SeparatorVisibility="None"
                                    behaviors:ListViewBehavior.NoBackgroundSelection="True">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <cardView:CardView Margin="40,15,40,15" HeightRequest="200"  CardViewOutlineColor="{StaticResource Primary}" Padding="5" CardViewOutlineColorThickness="1" CardViewHasShadow="True">
                                            <cardView:CardView.CardViewContent>
                                                <StackLayout Orientation="Vertical"  BackgroundColor="White" >
                                                    <StackLayout VerticalOptions="CenterAndExpand" >
                                                        <StackLayout Grid.Row="0" Grid.Column="0"  >
                                                            <StackLayout   HorizontalOptions="Center">
                                                                <Label Text="Enable Mobile Alert" FontSize="Small" TextColor="{StaticResource LightTextColor}"/>
                                                                <Switch IsToggled="{Binding PushNotification}" HorizontalOptions="Center">
                                                                    <Switch.Behaviors>
                                                                        <behaviors:EventToCommandBehavior EventName="Toggled" Command="{Binding ToggleAlertCommand}" Converter="{StaticResource ToggledConverter}" />
                                                                    </Switch.Behaviors>
                                                                </Switch>
                                                            </StackLayout>
                                                        </StackLayout>
                                                    </StackLayout>
                                                </StackLayout>
                                            </cardView:CardView.CardViewContent>
                                        </cardView:CardView>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
Run Code Online (Sandbox Code Playgroud)

GaragePageViewModel.cs

public ICommand ToggleAlertCommand { get; private set; }

public GaragePageViewModel()
    {
        ToggleAlertCommand = new Command<Vehicle>(ToggleMobileAlert);
    }

private async Task ToggleMobileAlert(GarageVehicle vehicle)
    {
       //do work
    }
Run Code Online (Sandbox Code Playgroud)

ToggledItemEventArgsConverter.cs

public class ToggledItemEventArgsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var eventArgs = value as ToggledEventArgs;
        return eventArgs.Value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

GaragePage.xaml.cs

   public GaragePage()
        {
            InitializeComponent();
            BindingContext = App.Container.Resolve<GaragePageViewModel>();
        }
Run Code Online (Sandbox Code Playgroud)

Dan*_* S. 5

您的问题和您的代码之间存在差异。您的代码显示您正在尝试将 Behavior 用作 AttachedProperty。

行为不是属性。行为被添加到 Behaviors 集合中,它是 VisualElement 的一个属性。您可以在此处查看如何使用 Behavior。该特定示例使用 Prism EventToCommandBehavior,它比 Xamarin 的示例有一些好处,例如您可以指定要发送到命令的 EventArgs 中的属性。

更新:

鉴于您想在更新诸如 Switch 之类的内容时触发命令,您可以执行以下操作(这再次使用 Prism EventToCommandBehavior)

<Switch IsToggled="{Binding Foo}">
    <Switch.Behaviors>
        <behavior:EventToCommandBehavior EventName="Toggled"
                                         Command="{Binding BindingContext.FooCommand,Source={x:Reference view}}"
                                         CommandParameter="{Binding .}"/>
    </Switch.Behaviors>
</Switch>
Run Code Online (Sandbox Code Playgroud)