Reputation: 3954
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
Upvotes: 3
Views: 3119
Reputation: 47
This work perfect !!
(Xamarin Forms Portable Solution)
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Hi");
MessagingCenter.Subscribe((App)Xamarin.Forms.Application.Current, "Hi", (sender) => {
labelStatus.Text = "We have the Hi message here";
});
Upvotes: -1
Reputation: 3954
As mentionned @AlessandroCaliaro by its Answer on Forums Xamarin
In OS side I use something like
Xamarin.Forms.MessagingCenter.Send<App> ((App)Xamarin.Forms.Application.Current, "OnBeaconServiceConnect");
In XF side I use this
MessagingCenter.Subscribe (this, "OnBeaconServiceConnect", (sender) => {
// do something
});
It's says -> Use something like
So
OS side: MainPage.xaml.cs
--> It's the WinPhone8.1 Part Below, but it should be the same for others plateforms (Android, iOS, Windows, UWP)
MessagingCenter.Send<Project.App>((Project.App)Xamarin.Forms.Application.Current, "Hi");
XF side: ListenerPage.xaml.cs
MessagingCenter.Subscribe<App>((App)Application.Current, "Hi", (sender) => {
Debug.WriteLine("get hi !!!");
});
Thank !
Upvotes: 3
Reputation: 3228
TSender
can be any class. It describes the event callback's message payload. For example, if you just wanted to send a string to subscribers then you would use Send<string>()
and Subscribe<string>()
. Or you can send more complex data structures... like maybe Send<Person>(newContact, "added")
and Subscribe<Person>(this, "added", (newContact) => { //... })
.
It is implemented as a Type
reference so that you can use private classes for TSender
, which basically means that nobody from outside of the declaring assembly can eavesdrop (private classes are not visible outside of the declaring assembly).
Upvotes: 1