Search code examples
xamarin.formsdependency-injectionunity-container

Haw to do the injectionParamter using Microsoft.Practices.Unity in xamrain forms


i'm developping xamarin forms app using MVVM architecture, in the beginning, i seperated the views, models, viewmodels in folders inside the PLC project. when i generate apk ,it's work fine on device or others cloud emulator. BUT, i was obligated to seperate the folders in assemblies (ViewModels, services ... ) and unsing Unity IOC injection of dependecy.

unityContainer = new UnityContainer();
unityContainer.RegisterType<IService, Service>();
unityContainer.RegisterType<IViewModel, ViewModel>();

the viewModel have a parameter public ViewModel(string proxy, IService service)

so in the page.xamal.cs , i did:

BindingContext = App.unityContainer.Resolve<ViewModel>(new ParameterOverride("proxy",proxy));

when i launch the app on emulator it'work fine, but there is an exception on device when launching the apk :

System.ArgumentNullException: Parameter type inference does not work for null values. Indicate the parameter type explicitly using a properly configured instance of the InjectionParameter or InjectionParameter classes.


Solution

  • i found the solution, i just add how to manage the nullable value

    BindingContext = App.unityContainer.Resolve<ViewModel>(new ParameterOverride("proxy",proxy != null ? proxy : string.Empty));