【问题标题】:MvvmCross Windows Phone 8.1 bind list selection to command, compilation failureMvvmCross Windows Phone 8.1 将列表选择绑定到命令,编译失败
【发布时间】:2016-05-05 12:10:53
【问题描述】:

我有以下错误:

Windows.UI.Xml.Controls.ListView 类型的对象无法转换为 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 的引用将交互添加到项目中。

当然,删除 &lt;i:Interaction.Triggers&gt; 块可以消除错误,但我需要将选择绑定到命令,就像我在 UIKit 和 Android 中所做的那样。

那么——这是什么魔鬼?

【问题讨论】:

  • 为什么不能将 SelectedItem 绑定到 ViewModel 中的属性?
  • 如果我有一个 setter 调用 ShowViewModel 比 ViewModel 的界面不太直观。似乎 Daily Dilbert 示例在 iOS 中使用 ItemSelectedCommand 而不是 Windows Store。如果我弄清楚为什么触发器不起作用,我会在这里发布答案,但我可能会以某种方式结束核心库。

标签: windows xaml windows-runtime mvvmcross


【解决方案1】:

问题是这段代码与 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 添加一个属性,就像这样——&lt;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中使用的相同类型的选择命令兼容。

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多