在 UWP 中将 Ui 事件绑定到命令 [英] Binding Ui-Events to Commands in UWP

查看:68
本文介绍了在 UWP 中将 Ui 事件绑定到命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将任何 UI 事件直接绑定到我的 ViewModel 中的命令.有没有办法在 UWP 中做到这一点?

I would like to bind any UI-Event directly to a command in my ViewModel. Is there a way to do that in UWP?

我正在使用 UWP 社区工具包,并希望将 Hamburger-Menu-Item-Click 绑定到一个命令.

I'm using the UWP Community Toolkit and would like to bind the Hamburger-Menu-Item-Click to an command.

谢谢托比亚斯

推荐答案

好的,我找到了解决方案.您可以在这里找到它:UWP 中的行为(事件触发命令错误

Ok I found the solution for that. You can find it here: Behaviors in UWP (Event trigger command error

我将在这里重复我的步骤:

I will repeat my steps here:

1 - 安装包

Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed

2 - 包括对您视图的引用

2 - Include references to you view

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

3- 将命令添加到您的 ViewModel

3- Add the command to your ViewModel

        public DelegateCommand MenuItemCommand { get; set; }

        public void InitCommands()
        {
            MenuItemCommand = new DelegateCommand(MenuItemClicked);                            
        }

        private void MenuItemClicked()
        {
            //do something when clicked
        }

4 - 将菜单项的 Click-Event 绑定到视图中的命令(参见 i:...-tag)

4 - Bind the Click-Event of the menuitem to the command in the view (see the i:...-tag)

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <controls:HamburgerMenu x:Name="HamburgerMenuControl"
                            PaneBackground="Black"
                            Foreground="White"
                            ItemTemplate="{StaticResource DefaultTemplate}"
                            OptionsItemTemplate="{StaticResource DefaultTemplate}"
                            ItemsSource="{Binding MenuItems}"
                            OptionsItemsSource="{Binding MenuOptionItems}"
                            >
        <i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="ItemClick">
                <core:InvokeCommandAction Command="{Binding MenuItemCommand}"></core:InvokeCommandAction>
            </core:EventTriggerBehavior>
        </i:Interaction.Behaviors>
        <Frame x:Name="ContentFrame"/>
    </controls:HamburgerMenu>
</Grid>

当我知道点击菜单项时它

when I know click the menu-item it

这篇关于在 UWP 中将 Ui 事件绑定到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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