2017-08-15 122 views
0

我学习/ 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; 
     // } 
     //} 

    } 

} 
+0

余米无法做出esc键MSGBOX出现,如果你要帮我:) –

+0

“现在我希望它关闭的PLS问什么,你需要esc键“ - 什么是”它“? –

+0

应用程序它自己,程序,整个事情(我有另一个项目在Esc的登录窗口消失,但仍然运行在任务管理器,我想能够正确关闭它) –

回答

1

在这种情况下,最简单的方法是使用预定义的ApplicationCommands.Close命令

这应该是这样的

<Window.InputBindings> 
    <KeyBinding Key="Esc" Command="ApplicationCommands.Close" /> 
</Window.InputBindings> 

然后在代码隐藏中

this.CommandBindings.Add(
    new CommandBinding(
    ApplicationCommands.Close, 
    (s,a)=>Application.Current.Shutdown() //or this.Close() depending on what you want to close 
) 
); 

其他的选择包括执行使用该ICommand接口或使用数百个提供这种功能,如prism's Delegate Command库的一个或Relay command

编辑自定义类

因为你不熟悉的匿名委托后面的代码也可以写成这样

​​
+0

谢谢...我尝试了很多不同的这样的方法,尝试Application.Close和App.Close和this.Close等...你能解释一下这条线做什么? :(s,a)=> Application.Current.Shutdown(),为什么我们在ApplicationCommands.Close之后需要它?什么是(s,a)? –

+1

'(s,a)=>'定义了一个匿名委托,它在这种情况下接受2个参数object和eventAgs e,标准事件处理指纹,Application.Current获取当前运行的应用程序,shuddown方法是关闭所选应用程序的命令 – MikeT

+0

如果我打扰你,我很抱歉,但是程序如何知道s是一个对象类型? a是eventArgs?他们在哪里指定?对我来说,它看起来像是一些黑魔法(s,a)=>部分(但我想理解,不仅仅是实现) –

1

您可以使用键绑定,示例代码如下图所示:

<InputBindings> 
    <KeyBinding Key="Esc" Command="{Binding CloseCommand}"/> 
</InputBindings> 

在window.xaml(恐怕就得背后写一些代码:()

Window(){ 
    InitializeComponent(); 
    this.DataContextChanged += (sender,e){ 
    var vm = e.NewValue as WindowViewModel; 
    if(vm != null){ 
     vm.CloseFunc =() => this.Close(); 
    } 
} 

}

对于WindowViewModel: 公益行动CloseFunc {获取;集;}

private RelayCommand _closeCommand; 
public RelayCommand CloseCommand => 
    _closeCommand??(_closeCommand = 
    new RelayCommand(() =>{ 
     CloseFunc?.Close(); 
    }))); 
+0

嗨,感谢您的帮助,但不能我像使用任何其他MVVM“命令”一样使用Binding CloseCommand:[code] private string seletedElement; public string SelectedElement { get {return seletedElement; } set { seletedElement = value; MessageBox.Show(seletedElement); } } [code] –

+0

@BasicHumanBeing命令绑定是你将如何处理任何其他命令,但处理该绑定的方法可以以不同的方式完成 – MikeT

+0

@Jake,我在windiw.xaml和WindowViewModel。在WVM中:操作和relaycommands命名空间找不到,在window.xaml.cs(我假设你指示我写它的地方)之后,发件人e)IDE删除了一个“=>”,并且它不识别CloseFunc(也我从来没有见过这个,它是做什么的:=()=>不是一个简单的this.close应该做到这一点?) –