Xamarin表示EventToCommandBehavior

时间:2017-05-11 05:36:41

标签: c# xamarin xamarin.forms

我一直在尝试按照位于以下位置的EventToCommandBehavior的示例进行操作:

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

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

我确实确保从" ItemsSelected"更改EventName。 To" Toggled" (切换事件)但事件根本没有被击中。我能做错什么?

FWIW,我试图遵循MVVM模式并最小化"背后的代码"

GaragePage.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>

GaragePageViewModel.cs

public ICommand ToggleAlertCommand { get; private set; }

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

private async Task ToggleMobileAlert(GarageVehicle vehicle)
    {
       //do work
    }

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

GaragePage.xaml.cs

   public GaragePage()
        {
            InitializeComponent();
            BindingContext = App.Container.Resolve<GaragePageViewModel>();
        }

1 个答案:

答案 0 :(得分:3)

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

行为不是属性。行为被添加到Behaviors集合,这是VisualElement上的一个属性。您可以看到here如何使用行为。该特定示例使用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>