windows - MvvmCross Windows Phone 8.1 绑定(bind)列表选择到命令,编译失败

标签 windows xaml windows-runtime mvvmcross

我有以下错误:

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> block 消除了错误,但我需要将选择绑定(bind)到命令,就像我在 UIKit 和 Android 中所做的那样。

那么——这是一种怎样的恶作剧方式?

最佳答案

问题是此代码与 Windows Phone 8.1 不兼容。

ListView 需要像这样设置:

<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"

这会导致另一个问题;虽然它将绑定(bind)到 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 中使用的同类选择命令兼容。

关于windows - MvvmCross Windows Phone 8.1 绑定(bind)列表选择到命令,编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050295/

相关文章:

C# WebView 不理解 alert() 的 JavaScript 调用(Windows 8 XAML 应用程序)

wpf - XAML - 如何拥有全局 inputBindings?

windows - 拆卸卸载的模块

windows - 如何关闭打开的 OLE 对话框

python - 获取 session 用户

c# - 如何从tabitem wpf的数据模板中找到控件

c# - 当第二列的内容可见时,如何将 Xaml 网格列 50/50 拆分?

c# - 存储文件到系统 uri?

c++ - 从 Windows::Storage::Streams::IBuffer 中获取字节数组

c# - 如何在 Windows (8.1) 中将自定义应用程序注册为 Web 浏览器?