Search code examples
c#android.netxamarinmessagingcenter

Xamarin MessagingCenter callback not being called


I am implementing device orientation detector using Xamarin MessagingCenter. What I would like to do is to send messages from my MainActivity in Android project to a Singleton class implementation inside my .NET Standart project.

As you can see I have overridden "OnConfigurationChanged(...)" method inside my MainActivity and all breakpoints are being hit inside my IF statements when I switch orientation from Landscape to Portrait. The problem is that I newer receive those messages. Callback inside my "OrientationHelper" is newer called.

"OrientationHelper" is instanciated when first page loads (for those who will say that I have no instance:) )

MainActivity:

public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
    base.OnConfigurationChanged(newConfig);

    if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
        MessagingCenter.Send(this, "OrientationContract"
            , new OrientationChangedEventArgs(Orientation.Landscape));

    else if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
        MessagingCenter.Send(this, "OrientationContract"
            , new OrientationChangedEventArgs(Orientation.Portrait));
}

Singleton class that will receive messages from MainActivity:

public class OrientationHelper
{
    private OrientationHelper()
        => MessagingCenter.Subscribe<OrientationChangedEventArgs>(this, "OrientationContract"
            , s => DeviceOrientation = s.Orientation);

    private static OrientationHelper s_instace;
    public static OrientationHelper Instance
    {
        get
        {
            if (s_instace == null)
                s_instace = new OrientationHelper();
            return s_instace;
        }
    }

    private Orientation _deviceOrientation;
    public Orientation DeviceOrientation
    {
        get => _deviceOrientation;
        private set
        {
            if (_deviceOrientation == value)
                return;
            _deviceOrientation = value;
        }
    }
}

OrientationChangedEventArgs:

public class OrientationChangedEventArgs : EventArgs
{
    public Orientation Orientation { get; private set; }

    public OrientationChangedEventArgs(Orientation orientation)
        => Orientation = orientation;
}

Solution

  • the subscribe and send methods are defined like this

    • Subscribe (object subscriber, string message, Action callback, TSender source = null)

    • Send (TSender sender, string message) Send (TSender sender, string message, TArgs args)

    the first T parameter in both calls should match the type of the class sending the message

    MessagingCenter.Send<MyType, OrientationChangedEventArgs>(this, "OrientationContract"
            , new OrientationChangedEventArgs(Orientation.Landscape));
    
    MessagingCenter.Subscribe<MyType, OrientationChangedEventArgs>(this, "OrientationContract"
            , s => DeviceOrientation = s.Orientation);