我学习/ C#中练习基本的MVVM框架解决方案应用新手MVVM在C#试图关闭与Esc键(操作键命令)
我有一个小程序,用一个ComboBox发挥各地,如果用户选择的东西从框中,它显示在一个MsgBox中。现在我想让它关闭esc键。我发现很多的解决这里的问题有关,像这样的:
Keyboard events in a WPF MVVM application? Press Escape key to call method
但我无法实施任何这些......我不甚至似乎能的KeyPreview设置为True(我现在只需写入表单,但有趣的是,我不能让它工作。)
我的问题是,我没有使用c#一段时间,林不知道该怎么用(KeyEventArg KeyEventHandler,我应该使用e.Key,e.keyDown?)和Im不知道该把代码放在哪里。我读了一些关于如何在XAML文件中处理它的东西,这将是最好的,但无法做到。现在这里是我在App.xaml.cs中的代码,我试图在不同的地方实现它,但我反思问什么时候编码,不知道该怎么做/什么Mi正在做,所以在这里。
我现在代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WpfComboBoxStrinbgListMVVM
{
public class MainViewModel
{
public MainViewModel()
{
ItemList = new List<string> { "item1", "item2", "item3" };
}
public List<string> ItemList { get; set; }
private string seletedElement;
public string SelectedElement
{
get { return seletedElement; }
set
{
seletedElement = value;
MessageBox.Show(seletedElement);
}
}
private void EscKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
MessageBox.Show("Escape key pressed");
// mainWindow.Close();? MainWievModel.Close(); App.Close();
}
}
//private void Form1_KeyDown(object sender, KeyEventArgs e)
//{
// if (e.KeyCode == Keys.Escape)
// {
// MessageBox.Show("Escape key pressed");
// // prevent child controls from handling this event as well
// e.SuppressKeyPress = true;
// }
//}
}
}
余米无法做出esc键MSGBOX出现,如果你要帮我:) –
“现在我希望它关闭的PLS问什么,你需要esc键“ - 什么是”它“? –
应用程序它自己,程序,整个事情(我有另一个项目在Esc的登录窗口消失,但仍然运行在任务管理器,我想能够正确关闭它) –