Xamarin Forms - MessagingCenter between two differents Pages/Projects

Emixam23Emixam23 USMember ✭✭✭
edited June 2016 in Xamarin.Forms

Hi,

I'm searching to implement MessagingCenter for X reasons into my Xamarin Forms (Portable Project).

-I have to make communicates the MainPage.xaml.cs of the Project.WinPhone (Windows Phone 8.1) with the Pages/MyPersonalPage.xaml.cs of the Project (PCL Part)

I think I have to use the both following function:


Send : Send<TSender> (TSender sender, string message)

Subscribe : Subscribe<TSender> (object subscriber, string message, Action<TSender> callback, TSender source = null)


Maybe I misunderstood but, if <TSender> is in the WinPhone project and subscriber is in the Portable project, I can't link them about assemblies. (Infinite two ways link)

It means I can't either access MainPage.xaml.cs or Pages/MyPersonalPage.xaml.cs. Then, how should I do to make it works?

My code is already ok otherwise, I'm able to catch the Events that I need from the WinPhone part and I'm also able to make the modification I need in the PCL. The only problem is I need to call this method which makes the modification once the Event comes up

Best Answers

Answers

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Could you please post both parts of your code?

  • Emixam23Emixam23 USMember ✭✭✭

    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;
            //MessagingCenter.Send<MainPage> (Project.Pages.ListenerPage, "false");
        }
        else
        {
            Project.App.SoftwareButtonEnable = true;
            //MessagingCenter.Send<MainPage> (Project.Pages.ListenerPage, "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.

    In the Xamarin PCL part, in the ListenerPage.xaml.cs, I have this code in the Constructor

    public ListenerPage()
    {
        InitializeComponent();
        //.... 
        MessagingCenter.Subscribe<this> (WinPhone.MainPage, "Hi", (sender) => {
            // I got a message
        });
    }
    
  • Emixam23Emixam23 USMember ✭✭✭
    edited June 2016

    @AlessandroCaliaro I get 2 errors.. Sorry :/

    OS side: MainPage.xaml.cs

    MessagingCenter.Send<App>((App)Xamarin.Forms.Application.Current, "Hi");

    Cannot convert type 'Xamarin.Forms.Application' to 'Project.WinPhone.App'


    XF side: ListenerPage.xaml.cs

    MessagingCenter.Subscribe(this, "Hi", (sender) => {
        Debug.WriteLine("get hi !!!");
    });
    

    The type arguments for method 'MessagingCenter.Subscribe<TSender, TArgs>(object, string, Action<TSender, TArgs>, TSender) cannot be inferred from the usage. Try specifying the type arguments explicitly.'

  • Emixam23Emixam23 USMember ✭✭✭

    Yes @ThomasBurkhart you're right, I just didn't understand it well and then, I tried to use as template each other in the both side.

    I completly misunderstood the TSender/Subscriber logic..

Sign In or Register to comment.