.net - Novice MVVM in C# trying to close application with Esc (handling key commands) -


i'm learning/practicing basic mvvm framework solutions in c#

i have small program plays around combobox, if user select box, displayed in msgbox. want close on esc key. found lot of solved questions here these:

keyboard events in wpf mvvm application? press escape key call method

but i'm unable implement of these... dont seems able set keypreview true (im okey write in form now, funny thing is, can't make work.)

my problems are, did not use c# while, im not sure use (keyeventarg keyeventhandler, should use e.key, e.keydown?) , im not sure put code. read few things how handel in xaml file, best unable it. right here code in app.xaml.cs, tried implement in various places reather ask when im coding , dont know do/what mi doing exactly, here im.

my code right now:

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         {             { 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 handling event         //        e.suppresskeypress = true;         //    }         //}      }  } 

the simplest method in case use predefined applicationcommands.close command

this this

<window.inputbindings>     <keybinding key="esc" command="applicationcommands.close" /> </window.inputbindings> 

then in codebehind

this.commandbindings.add(   new commandbinding(     applicationcommands.close,     (s,a)=>application.current.shutdown() //or this.close() depending on want close   ) ); 

other options include implementing custom class uses icommand interface or using 1 of hundreds of libraries provide functionality such prism's delegate command or relay command

edit

as not familiar anonymous delegates code behind written this

this.commandbindings.add(   new commandbinding(     applicationcommands.close,     performclose   ) );  public void performclose(object sender, executedroutedeventargs args) {     application.current.shutdown(); } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -