Need to get message back to Forms PCL from Android Prj

GenCodeGenCode USMember ✭✭

I have a RadiusNetworks IBeaconAndroid running in my main android project, works great.
But when I need to send an event back to the UI when a beacon comes in range.
I tried to use MessagingCenter to send a message from Androd prj to the Forms PCL prj but it would cause a recursive

How can I do this?
I don't think I need a custom render since I just want to use normal forms label.



  • CraigDunnCraigDunn USXamarin Team Xamurai

    Why would it 'cause a recursive'? You can register to receive messages OnAppearing of pages that need to handle it, and de-register OnDisappearing? The code on the page can then update the UI when a message is received (probably using Device.BeginInvokeOnMainThread).

  • EdScott.3010EdScott.3010 USMember ✭✭

    It causes recursive because Android prj have ref to the Forms pro
    so you can't make a ref from Forms to Android to use MessageCenter going that way

  • GenCodeGenCode USMember ✭✭

    Thanks Craig for the reply, let me try to explain a little more, I need to get the message from the Android prj back to my Form project


    public class MainActivity : AndroidActivity, IBeaconConsumer
    void RangingBeaconsInRegion(object sender, RangeEventArgs e)
    // send message back to Forms PCL so it can change text/colors when in range
    MessagingCenter.Send<MainActivity,string> (this, "beacon in range","beacon in range");

    Problem is there is no way to send it back to there is no way for the Forms PCL to subscribe to this message at all since it can't reference the Android prj

    Attached screenshot shows I can't subscribe to AndroidActivity.

    Now I can go the other way no issues at all.

    Does that help?

  • GenCodeGenCode USMember ✭✭

    Craig, thanks so much, my issue after seeing your example was I was trying to use "MainActivity" in the subscribe on the Forms, I thought thats the way it was :-(

    thats why I was trying to ref the Android prj

    what I had
    MessagingCenter.Subscribe <MainActivity, string>(this, "Hi", (sender, arg) => {

    All fixed up now :)
    MessagingCenter.Subscribe <Monitor, string>(this, "Hi", (sender, arg) => {

    Once again I can't thank you enough and you guys did a great job @Evolve.


  • rarenivarrarenivar USMember ✭✭

    I'm currently trying to implement this. I tried Craig's sample code but I'm getting an exception....

    09-29 14:45:17.334 I/MonoDroid(29292): UNHANDLED EXCEPTION: 09-29 14:45:17.344 I/MonoDroid(29292): System.InvalidCastException: Cannot cast from source type to destination type.
    see my post here

    Also, according to the documentation, aren't you suppose to specify the sender in the subscribe and sender methods? Which in @GenCode case would be MainActivity?

  • Sorry to jump in on this but can I ask why you would use the messaging system to do this unless your android project was a standalone app and the forms project needed to receive messages from it unless this is what you ARE doing.

    Basically am I doing it wrong because I do the following to get the data back:-
    public static void AlarmEvent(Object s, byte[] args) { App app = (App)Xamarin.Forms.Application.Current; Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { app.SetAlertPage(s, args); }); }
    AlarmEvent is actually a callback from a seperate thread:-
    NetScanShared.AlertEvent += AlarmEvent;

  • Emixam23Emixam23 USMember ✭✭✭
    edited June 2016

    Craig, I'm sorry to up the post but I don't understand how you deal with MessagingCenter..

    I'm trying to update my layout (Xamarin.Forms) from an Event in (Xamarin.Forms.WinPhone) in the WinPhone 8.1 part.

    MainActivity.xaml.cs (Project.WinPhone (Windows Phone 8.1))

    in the constructor, before loadApplication();

    if (ApplicationView.GetForCurrentView().DesiredBoundsMode == ApplicationViewBoundsMode.UseVisible)
        Project.App.SoftwareButtonEnable = false;
        ApplicationView.GetForCurrentView().VisibleBoundsChanged += OnVisibleBoundsChanged;

    I then have this method, after the constructor:

    private void OnVisibleBoundsChanged(ApplicationView av, object o)
        if (Project.App.SoftwareButtonEnable)
            Project.App.SoftwareButtonEnable = false;
            Project.App.SoftwareButtonEnable = true;
        Debug.WriteLine("private void OnVisibleBoundsChanged(ApplicationView av, object o)");
        Debug.WriteLine("New mod: " + ((Project.App.SoftwareButtonEnable == true) ? ("Visible") : ("Not Visible")));

    With this method, I can handle the appearance of the software buttons of some WinPhone. I would like to send a message, from the WinPhone 8.1 part, to the current page which is a PCL Xamarin.Forms Portable.

    I need it to resize/add margin dinamicaly to a layout.. can you help me?

    Of course, everything is good on both side, juste need to make them communicate..

    Thank !

  • kingleroygkingleroyg ZAMember ✭✭

    @CraigDunn Where Do you get GetMainPage()

    var mp = App.GetMainPage(); from ?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @kingleroyg that post is from 2014 and way out of date. Xamarin.Forms now uses

    Forms.Init(); // Forms.Init(this, bundle); on Android
    LoadApplication(new App());

    In each application project. Check out the updated MessagingCenter sample

Sign In or Register to comment.