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

Mohamed Ben Ayed 96 Reputation points
2021-01-08T10:14:04.397+00:00

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.

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Mohamed Ben Ayed 96 Reputation points
    2021-01-08T10:50:41.027+00:00

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

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

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.