.net - Is this the right way to shutdown prism application without default exit button? -


the window of app not have boarder, there no exit button on right conner?how can close in right way?

this way, fisrt bind command custom exit button.

<button content="exit" horizontalalignment="left" margin="327,198,0,0" verticalalignment="top" width="75" command="{binding exitcommand}"/> 

than throw exception in viewmodel when button clicked.

class viewmodel:notificationobject {     public viewmodel()     {         this.exitcommand = new delegatecommand(new action(this.executeexitcommand));     }      public delegatecommand exitcommand { get; set; }      public void executeexitcommand()     {         throw new applicationexception("shutdown");     } } 

catch exception in application class

public partial class app : application {     protected override void onstartup(startupeventargs e)     {         base.onstartup(e);         bootstrapper bootstrapper = new bootstrapper();         appdomain.currentdomain.unhandledexception += appdomainunhandledexception;         try         {             bootstrapper.run();         }         catch (exception ex)         {             handleexception(ex);         }     }      private static void appdomainunhandledexception(object sender, unhandledexceptioneventargs e)     {         handleexception(e.exceptionobject exception);     }      private static void handleexception(exception ex)     {         if (ex == null)             return;         environment.exit(1);     } } 

just maybe use application.current.shutdown() ??

public void executeexitcommand() {     application.current.shutdown(); } 

using exception communication mechanism seems weird.

if not want invoke shutdown() in vm whatever reason, use messenger(in prism that's eventaggregator) send custom message can subscribe application class or mainwindow's code-behind , invoke same application.current.shutdown()


Comments