问题描述:

I'm still in the early days of MVVM Light and tried researching this

I have a WPF app that is using pages. One page I have is called AVOPage and behind it is a ViewModel called AVOViewModel.

Basically all I want to do is every time I create the page the view model is created. When I close the page the ViewModel is destroyed. So basically I am using a new ViewModel every time for this page.

So how do I do this with the view model?

网友答案:

Solved my issue. In the ViewModelLocator

public AVOViewModel AVO
{
        get
        {
            if(!SimpleIoc.Default.ContainsCreated<AVOViewModel>())
                SimpleIoc.Default.Register<AVOViewModel>();

            return ServiceLocator.Current.GetInstance<AVOViewModel>();
        }
}

public static void Cleanup()
{
        // TODO Clear the ViewModels     
        if (SimpleIoc.Default.IsRegistered<AVOViewModel>())
            SimpleIoc.Default.Unregister<AVOViewModel>();
}

Finally in my the page close event

void Page_Close()
{
        PageFrame.Content = null;
        ViewModel.ViewModelLocator.Cleanup();
}
相关阅读:
Top