MvvmCross Windows Phone 8.1 绑定列表选择到命令,编译失败
MvvmCross Windows Phone 8.1 bind list selection to command, compilation failure
我有以下错误:
Object of type Windows.UI.Xml.Controls.ListView cannot be converted to type System.Windows.DependencyObject
由于以下代码:
<ListView Grid.Row="1" ItemsSource="{Binding Cases}" IsItemClickEnabled="False" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Left"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
我将 EventTrigger 添加到命名空间,如下所示:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
我通过从 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll
.
手动添加引用来将交互添加到项目中
当然,删除 <i:Interaction.Triggers>
块可以消除错误,但我需要将选择绑定到命令,就像我在 UIKit 和 Android.
中所做的那样
那么——这是什么鬼东西?
问题是这段代码不兼容 Windows Phone 8.1.
列表视图需要这样设置:
<ListView Grid.Row="1" ItemsSource="{Binding Cases}" IsItemClickEnabled="False" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Left"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ListView>
需要添加对 Behaviors
库的引用,而不是 Windows.Interaction。
以上代码要求根节点具有以下属性:
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
这会导致另一个问题;虽然它将绑定到 ShowCaseCommand,但它会传入 SelectionChangedEventArgs 的实例,而不是选定的列表项。
我们用CommandParameter
解决这个问题。
我们向 ListView
添加一个属性,就像这样 – <ListView Name='listView' ...
– 这个名称允许我们稍后引用它:
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=listView, Path=SelectedItem}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
通过像这样指定命令参数,我们可以将所选项目作为参数传递,使其与 iOS.
中使用的同类选择命令兼容
我有以下错误:
Object of type Windows.UI.Xml.Controls.ListView cannot be converted to type System.Windows.DependencyObject
由于以下代码:
<ListView Grid.Row="1" ItemsSource="{Binding Cases}" IsItemClickEnabled="False" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Left"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
我将 EventTrigger 添加到命名空间,如下所示:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
我通过从 C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll
.
当然,删除 <i:Interaction.Triggers>
块可以消除错误,但我需要将选择绑定到命令,就像我在 UIKit 和 Android.
那么——这是什么鬼东西?
问题是这段代码不兼容 Windows Phone 8.1.
列表视图需要这样设置:
<ListView Grid.Row="1" ItemsSource="{Binding Cases}" IsItemClickEnabled="False" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Subject}" HorizontalAlignment="Left"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</ListView>
需要添加对 Behaviors
库的引用,而不是 Windows.Interaction。
以上代码要求根节点具有以下属性:
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
这会导致另一个问题;虽然它将绑定到 ShowCaseCommand,但它会传入 SelectionChangedEventArgs 的实例,而不是选定的列表项。
我们用CommandParameter
解决这个问题。
我们向 ListView
添加一个属性,就像这样 – <ListView Name='listView' ...
– 这个名称允许我们稍后引用它:
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=listView, Path=SelectedItem}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
通过像这样指定命令参数,我们可以将所选项目作为参数传递,使其与 iOS.
中使用的同类选择命令兼容