Xamarin形成EventToCommandBehavior [英] Xamarin Forms EventToCommandBehavior

查看:60
本文介绍了Xamarin形成EventToCommandBehavior的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to follow the example for EventToCommandBehavior located at:

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

该示例显示了在列表视图中针对某个项目触发的selectedItem事件.我希望为列表视图中的任何项目触发一个Switch事件.

The example shows a selectedItem event being fired for an item in a list view. My hope is to fire a Switch event for any item in my list view.

我确实确保将EventName从"ItemsSelected"更改为"Toggled"(用于开关的事件),但是该事件根本没有被击中.我可能做错了什么?

I did make sure to change the EventName from "ItemsSelected" To "Toggled" (the event for the switch) but the event isn't hit at all. What could I be doing wrong?

FWIW,我正在尝试遵循MVVM模式,并尽量减少背后的代码"

FWIW, I'm trying to follow the MVVM pattern and minimize "code behind"

GaragePage.xaml

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

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

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

GaragePage.xaml.cs

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

推荐答案

您的问题和代码之间有区别.您的代码表明您正在尝试将行为"用作附加属性.

There is a difference between your question and your code. Your code shows you're trying to use a Behavior as an AttachedProperty.

行为不是属性.行为被添加到行为"集合中,该集合是VisualElement的属性.您可以在此处使用行为.该特定示例使用Prism EventToCommandBehavior,它比Xamarin中的示例具有一些优势,例如您可以指定要发送到命令的EventArgs中的属性.

Behaviors aren't properties. Behaviors are added to the Behaviors collection which is a Property on VisualElement's. You can see here how to work with a Behavior. That specific example uses the Prism EventToCommandBehavior which has a few benefits over the example from Xamarin like you can specify what the property is in the EventArgs that you want to send to your command.

更新:

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

Given that you want to trigger a Command when updating something like a Switch you could do the following (again this uses the 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>

这篇关于Xamarin形成EventToCommandBehavior的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆