This project has moved. For the latest updates, please go here.
2

Closed

Windows Phone 8 Unregister error

description

Hi,

I have been using the following code in a windows phone 7 application for some time ( using the binaries from WP71).

SimpleIOC.Default.GetInstance<viewModel>()
and
SimpleIOC.Default.Unregister(simpleIOC.Default.GetInstance<viewModel>())

In wp7 -
calling GetInstance - returns an instance of the viewmodel.
calling Unregister - removes the instance
calling GetInstance - returns a new instance.


however in WP8:
calling GetInstance - returns an instance of the viewmodel.
calling Unregister - removes the instance
calling GetInstance - returns the following error

{Microsoft.Practices.ServiceLocation.ActivationException: Type not found in cache without a key: MvvmLightTestApp.ViewModel.SecondaryViewModel
at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String key)
at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstanceTService
at MvvmLightTestApp.MainPage.Button_Click_1(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()

Any help would be appreciated

Thanks
Closed Feb 15, 2015 at 5:12 PM by lbugnion

comments

jpack61108 wrote May 4, 2013 at 6:26 PM

I am having kind of the same issue. Difference mine is on windows 8. My main page view model works perfectly and I have my secondary page view and viewModel set up the same way. When I navigate to the secondary page before the page loads I get this exception:

Type not found in cache:System.String

with this StackTrace:

at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String key) in d:\GalaSoft\mydotnet\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 532
at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetService(Type serviceType) in d:\GalaSoft\mydotnet\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 768
at GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstanceTClass in d:\GalaSoft\mydotnet\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 708

I have the latest binaries installed and Visual Studio 2012 is updated to update 2.

Please Help.

jpack61108 wrote May 4, 2013 at 6:48 PM

It appears as if it is not getting assigned a key in registration. I don't know just guessing.

Thanks for your hard work.

jpack61108 wrote May 4, 2013 at 7:05 PM

Nevermind I solved my own problem. I was trying to pass a string as a parameter to the constructor of my viewmodel and obviously I didn't have it worked out correctly with the container.

rtvg wrote Apr 23, 2014 at 12:14 PM

Hi,

I have a similar problem. When I do the following in a WP8 App, the app crashes with an ActivationException:
SimpleIoc.Default.Register<MyViewModel>();
var vm = SimpleIoc.Default.GetInstance<MyViewModel>();
SimpleIoc.Default.Unregister<MyViewModel>(vm);
vm = SimpleIoc.Default.GetInstance<MyViewModel>(); // <-- Exception
Can anyone tell me, if my understanding of the Unregister<T>(T instance) overload is wrong, or if this is still an unresolved bug? Maybe I'm using the methods in a way that is not supposed.

Any help or hint is very much appreciated. Thanks in advance.
~Raphael

rtvg wrote Apr 23, 2014 at 12:28 PM

edit: MyViewModel's constructor has an IDataService parameter and a class is registered with SimpleIoc for that interface.

lbugnion wrote Feb 6, 2015 at 2:01 PM

Fixed in V5.1 (upcoming)

lbugnion wrote Feb 6, 2015 at 4:13 PM

DLLs are available here:
http://1drv.ms/1ylP1TS

Nuget will be updated ASAP.