Resolving Xamarin Forms ´dependency service default constructor not found
I get this error with Visual Studio and Xamarin Forms with multiple connected Xamarin Projects (or assemblies).
The whole project structure is like this:
┌ LibraryProject ├ ─ ICustomView.cs ├ ─ Helper.cs // initialize CustomView_iOS with DependencyService ├ LibraryProject.Android ├ LibraryProject.iOS ├ ─ CustomView_iOS.cs // the custom platform dependency ├ MainProject ├ ─ MainPage.cs // use Helper.UpdateCustomViewText ├ MainProject.Android ├ MainProject.iOS └ ─ AppDelegate.cs
LibraryProject is a dependency of
MainProject. The class
Helper inside the
LibraryProject initialize the
CustomView_iOS class with the DependencyService:
The problem is that the
DependencyService cannot decide where to initialize the
So i force this calling with a dummy variable the
CustomView_iOS class in the right assembly and so forcing it to add to the compiler right order of dependencies.
Now the solution is to add to the class
CustomView_iOS the dummy
and calling in the app initialization inside the file
AppDelegate.cs which is in the assembly
Same problem with Custom Renderers
This problem resolves also the same problem with Custom Renderers when using multi project structure and calling the renderer inside the library project.
The structure is like that:
┌ LibraryProject ├ ─ CustomView.cs ├ LibraryProject.Android ├ LibraryProject.iOS ├ ─ CustomViewRenderer_iOS.cs ├ MainProject ├ ─ MainPage.cs ├ MainProject.Android ├ MainProject.iOS └ ─ AppDelegate.cs
The solution is to add the
x variable inside
CustomViewRenderer_iOS and call it in
AppDelegate to force to add to the list of file considered by the compiler.