Windows Phone 8.1 - 菜单弹出项单击命令 c# [英] Windows Phone 8.1 - Menu flyout item click command c#

查看:21
本文介绍了Windows Phone 8.1 - 菜单弹出项单击命令 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Windows Phone 8.1 编写一个应用程序,我想在 listView 项目上使用浮出控件.因为我正在尽我所能编写好的应用程序,所以我尝试使用 MVVM 模式和资源字典,其中包含一页中所有 xaml 的模板.但是,我无法绑定我的 MenuFlyoutItem 命令 - 它似乎没有看到页面的数据上下文,或者它有一些其他数据上下文.这是一些代码:

I am writing an application for Windows Phone 8.1, and I wanted to use a flyout on listView item. Because I am doing my best to write nice app, I am trying to use MVVM pattern and resource dictionaries with templates insead of all xaml in one page. However, I can't bind my MenuFlyoutItem Command - it seems like it doesn't see the datacontext of the page, or it has some other dataContext. Here is some code:

1) 我的模板在一个单独的资源字典中:

1) My template in a separate resource dictionary:

<Grid Margin="0, 0, 0, 10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>

        <Grid.Resources>
            <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
            <converters:EmptyDateConverter x:Key="EmptyDateConverter" />
        </Grid.Resources>

        <i:Interaction.Behaviors>
            <icore:EventTriggerBehavior EventName="Holding">
                <converters:OpenMenuFlyoutAction />
            </icore:EventTriggerBehavior>
        </i:Interaction.Behaviors>

        <FlyoutBase.AttachedFlyout>
            <MenuFlyout>
                <MenuFlyoutItem x:Uid="AddToCalendarMenuItem" Command="{Binding AddToCalendar}" />
            </MenuFlyout>
        </FlyoutBase.AttachedFlyout>

        <Image Grid.Column="0" Source="{Binding ThumbnailUri}"/>
        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="10,0,0,0">
            <TextBlock Text="{Binding Title}" Style="{StaticResource ListItemTitle}"/>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
                <TextBlock x:Uid="DvdReleaseDate" />
                <TextBlock Text="{Binding DvdRelease, Converter={StaticResource EmptyDateConverter}}" />
            </StackPanel>
        </StackPanel>
    </Grid>

2) 这是列表视图:

<ListView Grid.Row="1" x:Name="SearchListView"
                  ItemsSource="{Binding SearchList}"
                  ItemTemplate="{StaticResource SearchListTemplate}" SelectionChanged="NavigateToMovieDetails"  />

我的 ViewModel 是 app.xaml.cs 中的一种静态单例

My ViewModel is a static kind of singleton in the app.xaml.cs

我尝试在 xaml 中创建 VM 的新实例,但没有成功 - 也许我做错了.

I've tried to create a new instance of the VM in xaml, but it didn't work - maybe I was doing smth wrong.

非常感谢您的帮助!提前致谢.

I would really appreciate Your help! Thanks in advance.

最好的问候,罗马

推荐答案

如果你坚持使用 <ItemTemplate> 你将不得不在你的 ViewModel 中的每个模型都有一个 Command,这不是理想.

If you stick with that <ItemTemplate> you will have to have a Command per every Model in your ViewModel, which is not ideal.

要设置单个命令并有一个命令参数,请参阅此 SO 教程我在此处输入的代码太多:

To set a single Command and have a CommandParameter see this SO Tutorial I made it is too much code to type here:

实现 ViewModel 单个命令使用命令参数

这篇关于Windows Phone 8.1 - 菜单弹出项单击命令 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆