Issue
I am trying to pass data from one page to the other page using Messaging Center but it seems not to me working.
What I have tried:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
async private void isClicked(object sender, EventArgs e)
{
MessagingCenter.Send<Page, string>(this, "Hi", "Data Sent");
await Navigation.PushAsync(new FirstPage());
}
}
This is a Send from a first page named (MainPage) after the send I navigate to the other Page called FirstPage where I want to retrieve / subscribe to the message
On the first page I have this code:
public partial class FirstPage : ContentPage
{
public FirstPage()
{
InitializeComponent();
ReceiveMessage();
}
public void ReceiveMessage()
{
MessagingCenter.Subscribe<Page, string>(this, "Hi", (sender, values) =>
{
lblLabel.Text = values;
});
}
}
This does not seem to be working.. What could be the issue.
Solution
You have to subscribe before you send. Messages are not queued, they are delivered immediately then disappear. It would be much easier to just pass the data via the constructor, but if you insist on using MessagingCenter
create the page
var page = new FirstPage();subscribe to the message
you are already doing this in the
FirstPageconstructorsend the message
MessagingCenter.Send<Page, string>(this, "Hi", "Data Sent");navigate to the page
await Navigation.PushAsync(page);
Answered By - Jason
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.