【问题标题】:Xamarin EventToCommand switch toggledXamarin EventToCommand 开关切换
【发布时间】:2018-04-13 16:29:08
【问题描述】:

您好,我正在尝试使用 EventToCommand,我使用了 prism 库,但遇到了如下所述的小问题。

MyTestSwitch_1 可以正常执行 ToggledCommand,但是当我尝试单击基于 listview 项目生成的 MyTestSwitch_ListView 时,它没有做任何事情,没有给出任何异常应用程序不会崩溃它只是忽略 EventToCommand 行为

有谁知道可能是什么问题。谢谢。

<StackLayout>
    <Switch x:Name="MyTestSwitch_1" IsToggled="{Binding ValueB}}">
        <Switch.Behaviors >
            <b:EventToCommandBehavior EventName="Toggled" 
                                      Command="{Binding ToggledCommand}"/>
    </Switch.Behaviors>
</Switch>

<ListView x:Name="MyListView"
        ItemsSource="{Binding ValuesList}"
        ItemTapped="Handle_ItemTapped"
        CachingStrategy="RecycleElement"

          HasUnevenRows="True"

        IsPullToRefreshEnabled="True"            
        RefreshCommand="{Binding RefreshCommand}"
        IsRefreshing="{Binding IsRefreshing, Mode=OneWay}"
          Margin="10">
    <ListView.Behaviors>
        <b:EventToCommandBehavior EventName="ItemTapped" 
                                  Command="{Binding ItemTappedCommand}"
                                  CommandParameter="MyParameter" />
    </ListView.Behaviors>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding Id}" 
               Style="{DynamicResource ListItemTextStyle}" />
                    <Label Text="{Binding Value}" 
               Style="{DynamicResource ListItemDetailTextStyle}"/>
                    <Switch x:Name="MyTestSwitch_ListView" IsToggled="{Binding ValueB}}">
                        <Switch.Behaviors >
                            <b:EventToCommandBehavior EventName="Toggled" 
                                  Command="{Binding  ToggledCommand}, Source={x:Reference ThisPage}"
                                  CommandParameter="{Binding .}" />
                        </Switch.Behaviors>
                    </Switch>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

【问题讨论】:

  • 我刚刚发现,当我将它添加到 itemtemplate 时,它​​甚至没有按钮,它只是忽略点击事件或切换事件......

标签: xamarin switch-statement behavior eventtocommand


【解决方案1】:

您需要参考 ViewModel。因为命令是它的属性。 像这样的:

Command="{Binding Path=ViewModel.PermissionGroupSelectCommand, Source={x:Reference invitationPage}}"

Source 引用当前页面的名称和 ViewModel 上的 Path 引用命令

【讨论】:

    【解决方案2】:

    你好,我终于在这个帖子找到了解决方案

    https://forums.xamarin.com/discussion/84190/listview-binding-command

    感谢 NMackay

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 2013-01-24
    • 2021-02-02
    相关资源
    最近更新 更多