{"id":223,"date":"2009-09-28T11:36:41","date_gmt":"2009-09-28T09:36:41","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=223"},"modified":"2009-09-28T11:36:41","modified_gmt":"2009-09-28T09:36:41","slug":"the-wpf-application-class","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=223","title":{"rendered":"The WPF Application Class"},"content":{"rendered":"<p>If you create a new WPF application project with VisualStudio, you get some basics for free. Well&#8230; almost for free. The price you pay is that you might not know what&#8217;s going on behind the scenes.<br \/>\n<!--more--><br \/>\nDon&#8217;t get me wrong. The default behavior works just fine for most simple applications, but there are some issues you might face when you&#8217;re trying to do something more sophisticated. The default behavior of an application is to simply quit as soon as the last window is closed. Works fine when you have a main window and some dialogs on top of that. But what if you want a login dialog BEFORE your main window is shown? Well. Tough luck. As soon as your login dialog is closed, your application shuts down.<\/p>\n<p>Of course there are ways to customize this behavior, but you have to know how &#8211; as usual.<\/p>\n<p>The first thing you have to do is set the ShutdownMode to &#8220;OnExplicitShutdown&#8221;. You can do this either directly in your App.xaml<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;Application x:Class=&quot;MyApp.App&quot;\r\n    xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\r\n    xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\r\n    ShutdownMode=&quot;OnExplicitShutdown&quot;&gt;\r\n<\/pre>\n<p>or alternatively in the code, for example in the OnStartup method of your application<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nprotected override void OnStartup(StartupEventArgs e)\r\n{\r\n    base.OnStartup(e);\r\n    ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;\r\n}\r\n<\/pre>\n<p>This way, your application will only terminate if you explicitly call the <code>Application.Current.Shutdown();<\/code> method. Of course, this also means, that if you &#8220;forget&#8221; to call the shutdonw method, you application won&#8217;t terminate.<\/p>\n<p>Another way to customize the application behavior is to directly change the <code>Main<\/code> method. But wait&#8230; there is no <code>Main<\/code> method&#8230; Actually, there is. If you compile your project and go to the obj folder, you will find an &#8220;App.g.cs&#8221; file with generated code that contains something like this<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Application Entry Point.\r\n\/\/\/ &lt;\/summary&gt;\r\n&#x5B;System.STAThreadAttribute()]\r\n&#x5B;System.Diagnostics.DebuggerNonUserCodeAttribute()]\r\npublic static void Main() {\r\n   MyApp.App app = new MyApp.App();\r\n   app.InitializeComponent();\r\n   app.Run();\r\n}\r\n<\/pre>\n<p>Of course you can&#8217;t (or better: shouldn&#8217;t) edit the generated code. Your changes would be overwritten the next time you compile your project. Instead, what you have to do is to change the &#8220;Build Action&#8221; of your App.xaml from &#8220;ApplicationDefinition&#8221; to &#8220;Page&#8221;. This way, the <code>Main<\/code> method will not be generated automatically and you can write your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you create a new WPF application project with VisualStudio, you get some basics for free. Well&#8230; almost for free. The price you pay is that you might not know what&#8217;s going on behind the scenes.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,97],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"has-more-link","5":"post-223","7":"format-standard","8":"category-csharp","9":"category-wpf"},"acf":[],"views":2873,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/223"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}