Wpf命令绑定在key.enter上退出程序 [英] Wpf commandbinding to exit the program on key.enter

查看:103
本文介绍了Wpf命令绑定在key.enter上退出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试让我的WPF应用程序随时向我显示一条消息,我按下'回车',

这就是我想要实现的目标每当我按Enter键时应用程序正在工作的任何时候给我一条消息或抛出一个异常,这样我就可以看到它正在读我的按键。

下面我把我的代码(因为我搜索了所以在上周我的问题很多解决方案它与我在SO,MSDN,Channel9等上发现的关于这个问题的代码非常相似)



我的XAML代码:

Hi,
I'm trying to make my WPF app to show me a message anytime that I press 'Enter',
This is what I want to Achieve anytime that the app is working whenever I press Enter It Gives me a message or throw an exception so I can see the It's reading my key press.
Below I put my code (As I've searched so many solutions for my problem within the last week It's very similar to the codes I've found about this problem on SO, MSDN, Channel9 and so on)

My XAML code:

<Window.InputBindings>
    <KeyBinding Command="{Binding throwExceptionTest}" Key="Enter"/>
</Window.InputBindings>





MainWindow代码:



MainWindow code:

public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }





ViewModel代码:



ViewModel code:

public class MyViewModel
   {
       private ICommand throwExceptionTest;

       public ICommand ThrowExceptionTest => throwExceptionTest?? (throwExceptionTest = new ActionCommand(() =>
                      {
                          MessageBox.Show("ThrowExceptionTest");
                      }));
   }





ICommand代码:



The ICommand code:

public class ActionCommand : ICommand
   {
       private readonly Action _action;

       public ActionCommand(Action action)
       {
           _action = action;
       }

       public void Execute(object parameter)
       {
           _action();
       }

       public bool CanExecute(object parameter)
       {
           return true;
       }

       public event EventHandler CanExecuteChanged;
   }





我在这里遇到的其他问题因为我是WPF的初学者,所以我不喜欢我不知道如何跟踪这个按键事件。



任何帮助,提示,评论都可能有所帮助。

提前,我很感激你花时间阅读这个问题并帮助我。

谢谢,



我尝试了什么:



过去一周我搜索了SO,MSDN,Channel9,YouTube但问题仍然存在。



The Other problem I have here As I'm a beginner in WPF is that I don't know how I can trace this keypress event.

Any help, hint, comment could be helpful.
In advance, I appreciate that you spend your time reading this question and helping me.
Thanks,

What I have tried:

I searched the past week over SO, MSDN, Channel9, YouTube and yet The problem still persists.

推荐答案

您将 MainWindow DataContext 设置为自身,因此您需要将以下内容添加到后面的 MainWindow 代码:

You're setting the DataContext of the MainWindow to itself, so you will need to add the following to the MainWindow code behind:
public ICommand MyCommand { get; }
    = new ActionCommand(() =>
    {
        Debug.WriteLine("==> Enter Key Pressed");
    });



然后将以下内容添加到您的MainWindow XAML:


Then add the following to your MainWindow XAML:

<Window.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding MyCommand}"/>
</Window.InputBindings>



现在当你按 Enter 键时,命令将会激活。


Now when you press the Enter key, the Command will fire.


这篇关于Wpf命令绑定在key.enter上退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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