Notify View (Models) closing the program

So, I got the prism / mvvm / mef program, which works well, the user enters data into the application, then closes the application (or shuts down the computer).

How can I get my view (model) notified of the program closing / shutting down the computer so that it can either save user data, or maybe ask if it should be saved?

Losing data about closing a program is something that should be avoided, and it makes no sense to save material on every keystroke.

+5
source share
1 answer

CompositeCommands, "", .

public static class HostCommands
{
    private static readonly CompositeCommand Shutdown = new CompositeCommand();

    public static CompositeCommand ShutdownCommand
    {
        get { return Shutdown; }
    }
}

shutdown ,

public Shell()
{
    InitializeComponent();

    Closing += (sender, e) =>
    {
        if (HostCommands.ShutdownCommand.CanExecute(e))
            HostCommands.ShutdownCommand.Execute(e);
    };
}

,

public SomeViewModel(IEventAggregator eventService)
{
    //blah, blah, blah...

    HostCommands.ShutdownCommand.
        RegisterCommand(new DelegateCommand<object>(_ => Save()));
}

, , . , CancelEventArgs, , , Cancel = true. Shell , , . .

+8

All Articles