Capturing OnExit events in WPF using MVVMCross

I need to explicitly close and delete the object SerialPortwhen my MVF MVVMCross application terminates, i.e. when clicking on the red X. For this, I need to call the method of the current ViewModel.

I tried to do this by following MVVMCross n = 42 video and adding an interface IKillableusing a method public abstract void KillMe()in the class abstract BaseViewModelthat implements IKillable. Then in the WPF project, I added a class BaseView, as in the video. At the moment, I can access the methods of the current view model.

In the video, examples are iOS, Droid and Windows Phone, where you can overrideevents, such as OnNavigatedToetc. In WPF there is no class OnExit()I could .overrideBaseView

I am wondering if there is a way I can name this method of the current view model when the application exits? I can do it:

protected override void OnExit(ExitEventArgs e)
{
    // Do Something
    base.OnExit(e);
}

in the class App.Xaml.Mvx.csthat comes with the MVVMCross Nuget package package, and I think this is the right place to put the code that I want to execute when the program exits. The problem is that I have no reference to the current view model.

If anyone can help me, I would really appreciate it. Thank!

EDIT / UPDATE:

, "", . , SerialPort, , MVVMCross. , singleton App.Xaml.Mvx.cs, DoSetup() Mvx.Resolve<>(); . protected override void OnExit(ExitEventArgs e), Close() Dispose() . , .

, , MVVMCross.

+4
1

, OnSuspending SerialPort , ViewModel, , ViewModel App.xaml.cs, OnSuspending()

, , , . , , .

0

All Articles