1
votes

I am having issues with Platform Effects in Xamarin Forms. These effects worked in a shared library format, and we are now migrating to .NET Standard 2.0 for reasons outside of the scope of this post.

Project Setup

  • VS 2017 15.8.0
  • Xamarin Forms 3.1.0.697729
  • Two .Net Standard 2.0 projects (one is for components, the other for the main UI)
  • Two Android projects (one that runs, the other is an Android .dll)
  • Two iOS projects (same deal)

Main Issue None of my platform effects in the Android .dll work. They all resolve as null effect.

According to the documentation, my setup is correct. (Documentation here)

I found two different issues and have hit a brick wall.

  1. When I follow the instructions exactly and place [assembly: ResolutionGroupName("MyGroupName")] on the platform renderer itself, I get exceptions with Xamarin's dependency resolver. There is no message, just an exception and stack trace that leads back to the constructor of the RoutingEffect.
  2. When I use a slightly different pattern and register that on the namespace of an empty file in my .NET Standard 2.0 project, all effects return as null effect.

I've been Googling and digging through forums to no avail. Does anyone have an idea what could be the problem?

Side notes: All of the custom renderers work after using the static Init() trick, but not the effects. I have also tried making the typeof() statement reference both the Android version of the class and the .NET Standard 2.0 version of the class.

For the sake of completeness, here's what I have going.

NET Standard 2.0:

namespace MyBaseNamespace.Components.Effects
{
    public class SearchBarStylingEffect : RoutingEffect
    {
        public SearchBarStylingEffect() : base("MyGroupName.SearchBarStylingEffect")
        {
        }
    }
}

Android:

[assembly: ResolutionGroupName("MyGroupName")]
[assembly: ExportEffect(typeof(SearchBarStylingEffect), "SearchBarStylingEffect")]
namespace MyBaseNamespace.Components.Android.Renderers.Effects
{
    public class SearchBarStylingEffect : PlatformEffect
    {
      …
    }
}

(iOS is the same as Android, only the namespace is different)

1

1 Answers

1
votes

I also posted this on Xamarin's forums, and Billy Liu from Xamarin asked a question that ultimately led me down the right path. I'll link to that here and also post what fixed it.

link: Xamarin Forms Post

Here's the resolution from that post:

In short, I had to do the following things to get this functional (which it now is):

  1. Ensure that the GroupNameResolution tag was on the first Effect ever initialized in the app.
  2. Remove the empty file initializer from my .Net 2.0 project.
  3. ???
  4. Profit.