Where is the Unregister in SimpleIoc

Oct 31, 2013 at 2:28 AM
Hi



I have a list of items that goes to another page, That page is hooked up to a view model. In the constructor of this view model I have code that grabs data from the server for that particular item.

What I found is that when I hit the back button and choose another item fromt hat list and it goes to the other page the constructor does not get hit.

I think it is because the VM is now created and thinks it does not need a new one. I am wondering how do I force a cleanup so that a fresh one is always grabbed when I select from my list?

I seem some people doing this
       SimpleIoc.Default.Unregister<ViewModelName>();
But I don't seem to have that and I am not sure how that ties into the cleanup method in the viewmodellocator?

Mvvm Light Version: 4.0.23.35541
Nov 3, 2013 at 9:34 AM
Edited Nov 3, 2013 at 9:35 AM
A workaround is to Get each time a new instance of ViewModel using Guid, so each time a new constructor of VM is called. See 2nd point of this good little tutorial: http://stackoverflow.com/questions/13795596/best-tutorial-to-learn-mvvmlight-simpleioc-use
SimpleIoc.Default.GetInstance<ViewModelName>(Guid.NewGuid().ToString());
I also haven't yet understand how to use the ViewModelLocator.Cleanup() ... :-(
Nov 5, 2013 at 9:19 AM
We're currently using Unity as we have to use bits of Prism as we load modules into a shell but I did some test harness apps for our data services and used SimpleIoC.

ViewModelLocator.Cleanup seems to work fine but the Cleanup in the viewmodellocator is for app termination rather than cleanup of specific VM instances (I think), seems to work that way anyway.
 public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            SimpleIoc.Default.Register<ISecurityDataService, SecurityDataService>();
            SimpleIoc.Default.Register<IDesktopDataService, DesktopDataService>();
            

            SimpleIoc.Default.Register<MainViewModel>();
            SimpleIoc.Default.Register<UserViewModel>();
            SimpleIoc.Default.Register<SecurityViewModel>();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        public UserViewModel User
        {
            get
            {
                return ServiceLocator.Current.GetInstance<UserViewModel>();
            }
        }

        public SecurityViewModel Security
        {
            get
            {
                return ServiceLocator.Current.GetInstance<SecurityViewModel>();
            }
        }

       public static void Cleanup()
        {

            // Implements IDisposable
            SimpleIoc.Default.Unregister<IDesktopDataService>();
            SimpleIoc.Default.Unregister<ISecurityDataService>();


            SimpleIoc.Default.Unregister<SecurityViewModel>();
            SimpleIoc.Default.Unregister<UserViewModel>();
            SimpleIoc.Default.Unregister<MainViewModel>();

        }    }
Nov 5, 2013 at 4:26 PM
Ya, I am able to do that maybe I was looking at the wrong area or something but I have the Unregister now. Thought what I noticed if I want to go back to the same view I need to add a check to re register the VM (which makes sense)

public MainViewModel Main
    {
        get
        {
           // do a check to see if a VM exists if not then re-register
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }
I notice you don't do that. Did you not run into this problem?
Nov 6, 2013 at 10:22 AM
chobo2 wrote:
Ya, I am able to do that maybe I was looking at the wrong area or something but I have the Unregister now. Thought what I noticed if I want to go back to the same view I need to add a check to re register the VM (which makes sense)

public MainViewModel Main
    {
        get
        {
           // do a check to see if a VM exists if not then re-register
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }
I notice you don't do that. Did you not run into this problem?
That sample app just has one instance of the VMs so it didn't matter. We don't use page navigation so our requirements are different.