jason
jason

Reputation: 7164

Keep WPF application alive on Closing

There are questions about this already, but this is a bit different so I'm asking. This is my xaml inside Window :

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
        <i:InvokeCommandAction Command="{Binding ClosingCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

This is closing Command :

public virtual ICommand ClosingCommand
{
    get { return new RelayCommand(ClosingExecute); }
}

This is Execute :

public virtual void ClosingExecute()
{
   MessageBoxResult result = MessageBox.Show("Close the application?", "Shut Down", MessageBoxButton.YesNo);

    if(result == MessageBoxResult.Yes)
    {
        Application.Current.Shutdown();
    }
    else
    {
       //I don't know what to write
    }

}

How can I keep my application alive in this situation? Thanks.

Upvotes: 1

Views: 1126

Answers (2)

Grigor Yeghiazaryan
Grigor Yeghiazaryan

Reputation: 197

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml"
    ShutdownMode="OnExplicitShutdown">
</Application>

ShutdownMode="OnExplicitShutdown"

Source: https://stackoverflow.com/a/9992888/3855622

Upvotes: 1

Tejas Sharma
Tejas Sharma

Reputation: 3440

From the documentation of Closing

Closing can be handled to detect when a window is being closed (for example, when Close is called). Furthermore, Closing can be used to prevent a window from closing. To prevent a window from closing, you can set the Cancel property of the CancelEventArgs argument to true.

You need to hook up an event handler to this event and set Cancel to true.

If you want to do this in an mvvmy way, this answer might help you hook up your eventargs to the command parameter

Upvotes: 2

Related Questions