0

您好我正在尝试使用 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>

4

2 回答 2

0

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

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

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

于 2022-01-18T18:16:56.523 回答
-1

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

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

感谢 NMackay

于 2018-04-14T13:34:14.850 回答