LightInject
LightInject copied to clipboard
Xamarin Forms Casting Exception in LoadApplication
`var container = new ServiceContainer();
container.Register<WardListPage>();
container.Register<WardListViewModel>();
container.Register<WardListService>();
container.Register<PatientListViewPage>();
container.Register<PatientListViewModel>();
container.Register<PatientService>();
container.Register<PatientDetailsViewPage>();
container.Register<PatientDetailsViewModel>();
var instance = container.GetInstance<WardListPage>();
MainPage = new **NavigationPage(instance);`**
Getting exeption : System.InvalidCastException: Specified cast is not valid. when calls LoadApplication(new App()); in Xamarin.Forms
when calling the instance as a NavigationPage this happens. when MainPage = instance it works fine.
Do you have any idea why this happens?
Could you provide a simple repro?
I don't understand what is happening here, but when registering a Xamarin.Forms page with the generic, container.Register<SomePage>(), vs using the factory, container.Register(x => new SomePage(x.GetInstance<SomeViewModel>()) the resulting ToolbarItems property on the Page is converted from an ObservableCollection<> to a List<> which then causes the InvalidCastException. I will try to get a simple repro made.
Here is a reproducible example: https://github.com/jordansjones/LightInjectErrorExample The main area of concern is here: https://github.com/jordansjones/LightInjectErrorExample/blob/de7334f17c67532acf37abc0a833c2632fbf1617/LightInjectErrorExample/LightInjectErrorExample/App.xaml.cs#L17-L34
Here is the ToolbarItems on the Xamarin.Forms Page that is getting changed: https://github.com/xamarin/Xamarin.Forms/blob/daa0589999171cfc153a915ca122b6e80d7948c4/Xamarin.Forms.Core/Page.cs#L130 And here is the initialization of that property: https://github.com/xamarin/Xamarin.Forms/blob/daa0589999171cfc153a915ca122b6e80d7948c4/Xamarin.Forms.Core/Page.cs#L62-L64
So it works when using the factory?
Yes. Meaning, the ObservableCollection isn't replaced with a List when using the factory.