0
votes

I created a Xamarin.Forms project/library. The project/library contains pages, views, renderers and other shareable components.

I then created my main project that will reference the shared project. I added each project (shared, android, ios) as an existing solution project.

The problem is when I try to use ShareProject's custom renderer in my MainShareProject, the implementation in SharedProjectAndroid and SharedProjectiOS is not called. When I call ISomeService in MainShareProject via DependencyService it is null

Solution Structure:

Shared Project Structure (Purpose of SharedProject is to reuse it in other projects)

ShareProject (NET standard library)

  • custom renderer

  • ISomeService

SharedProjectAndroid (Reference ShareProject; Xamarin.Android library)

  • custom renderer implementation

  • ISomeServiceImpl

ShareProjectiOS (Reference ShareProject; Xamarin.iOS library)

  • custom renderer implementation

  • ISomeServiceImpl

Main Project Structure

MainShareProject (Reference ShareProject)

  • use custom renderer (calls custom renderer constructor in ShareProject but not its implementation

  • use ISomeService by DependencyService (returns null)

MainProjectAndroid (Reference ShareProject and ShareProjectAndroid)

MainProjectiOS (Reference ShareProject and ShareProjectAndroid)

var someService = DependencyService.Get<SharedProject.ISomeService>(); //returs null

Android

[assembly: Dependency(typeof(SharedProjectiAndroid.SomeService))]
{
    public class UserPreference : ISomeService

}
1
Your description of the project structure is impossible to understand. Can you carefully describe the project structure for just one platform, ie Android?Jason
also, your registration should point to the implementation class, like "[assembly: Dependency(typeof(SharedProjectiAndroid.UserPreference))]"Jason
"The problem is when I try to use a custom renderer in my main project from the SharedProject, the implementation in SharedProjectAndroid and SharedProjectiOS is not called. Same for when trying to use DependencyService.". That's pretty much impossible, you need to describe what you are doing.Ivan Ičin
I updated the question with the structure of the project.PLOW

1 Answers

4
votes

I was fighting with the same problem that custom renderer from external assemblies are not called. I never investigated deeper, but it seems that it is somehow related to the code optimization.

For me the solution was to add an explicit call from my project to the external assembly.

Like this:

Assembly

public static class CoreUI
{
   public static void Init() { }        
}

Project

protected override void OnCreate(Bundle bundle)
{ 
   base.OnCreate(bundle); 
   CoreUI.Init(); // Initialisation of CoreUI
}