Home MVVM Light: Cleanup view and viewmodel
Reply: 0

MVVM Light: Cleanup view and viewmodel

BertAR
1#
BertAR Published in 2017-12-07 14:55:47Z

I am creating a tool which uses MMVM Light's ViewModelLocator, Messenger together with IOC.

One of my viewmodels constructor takes an integer as parameter, Action.TaskId.

When the window is closed, I would like to

  • release registered message handlers in the relevant view
  • unregister relevant viewmodel from cache (viewmodel should not be cached)

Questions

  1. I am calling the Cleanup method of my viewmodel from my view (via ViewModelLocator), but I am not sure that is the right way to go?
  2. In my viewModelLocator I like to cleanup (unregister) all my viewmodels. How?
  3. Will SimpleIoc.Default.Unregister<NewVersionViewModel>() properly unregister the viewmodel? Because SimpleIoc.Default.Register(() => new NewVersionViewModel(Action.TaskId)) is used to register the viewmodel.

XAML

<Window DataContext='{Binding NewVersion, Source={StaticResource Locator}}'>

ViewModelLocator

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register(() => new NewVersionViewModel(Action.TaskId)); //TaskId is an int
        SimpleIoc.Default.Register<SettingsViewModel>();
    }

    public NewVersionViewModel NewVersion
    {
        get
        {
            return ServiceLocator.Current.GetInstance<NewVersionViewModel>();
        }
    }

    public static void Cleanup()
    {
        //Sets the Messenger's default (static) instance to null.
        Messenger.Reset();
    }

}

ViewModel

public class NewVersionViewModel : ViewModelBase
{
    public static readonly Guid Token = Guid.NewGuid();

    public NewVersionViewModel(int taskId)
    {
        Messenger.Default.Register<NotificationMessage<string>>(this, Token, OnStringNotification);
    }

    private void OnStringNotification(NotificationMessage<string> obj)
    {
        //Do OnStringNotification

    }
     public override void Cleanup()
    {
        //remove from cache
        SimpleIoc.Default.Unregister<NewVersionViewModel>();

        //register again
        SimpleIoc.Default.Register((() => new NewVersionViewModel(Action.TaskId)));

        //Unregisters this instance from the Messenger class.
        base.Cleanup();
    }
}

Window

public partial class NewVersionWindow : Window
{
    public static readonly Guid Token = Guid.NewGuid();
    public NewVersionWindow()
    {
        InitializeComponent();
        Messenger.Default.Register<NotificationMessage>(this, Token, HandleNotification);
        Unloaded += (o, e) => { Messenger.Default.Unregister(this); }; //Ensure that the registered message handlers are released when the view is unloaded
    }

    private void HandleNotification(NotificationMessage message)
    {
        if (message.Notification == MvvmLight.NotMessageHelper.CloseWindow)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() => { Close(); });
        }
    }

    private void Window_Unloaded(object sender, RoutedEventArgs e)
    {
        var viewModelLocator = Application.Current.Resources["Locator"] as ViewModelLocator;
        viewModelLocator.NewVersion.Cleanup();            
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303753 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO