1

Hi Im trying to send multiple entries with messeging center but couldnt manage it (im new on xamarin and couldnt found proper examples for my code) im trying to idenify messages on confirm page (_entry1 you will go here _entry2 you will go there)

InformationPage Xaml

<Label Text="Please Type Informations Needed"  Margin="35" HorizontalOptions="Center"/>
<Entry x:Name="_entry1" Placeholder="Info 1"/>
<Entry x:Name="_entry2" Placeholder="Info 2"/>
<Button Text="Send Information" BackgroundColor="Crimson" TextColor="White" Clicked="SendInformation"/>

InformationPage CS

private void SendInformation(object sender, EventArgs e)
    {
        Navigation.PushAsync(new ConfirmPage());
        MessagingCenter.Send(this, "EnteryValue", _entry1.Text);
        MessagingCenter.Send(this, "EnteryValue", _entry2.Text);
    }

ConfirmPage CS

MessagingCenter.Subscribe<InformationPage, string>(this, "EnteryValue", (page, value) =>
{
        _confirm.Text = value;
        MessagingCenter.Unsubscribe<InformationPage, string>(this, "EnteryValue");
});
Henimex
  • 43
  • 6
  • why aren't you just passing the 2 values on the constructor of `ConfirmPage`? That would be much simpler than using `MessagingCenter` – Jason Oct 01 '20 at 11:50
  • i gave up... truly no updated resources for xamarin always struggling with outdated plugins old setups or all of them are irrelevant for a fresh start. sry for bothering you all. rerouting to Kotlin.. – Henimex Oct 01 '20 at 19:57
  • This is incredibly simple to fix, but as I noted above is an odd case to use MessagingCenter when a much more direct approach exists. – Jason Oct 01 '20 at 20:01
  • if you really want to use MessagingCenter, 1) your type arguments on send and subscribe need to match, 2) you are sending the same message with different payloads, so there is no way to determine which value is which, 3) you are unsubscribing after you receive a message, so you will never receive a 2nd message, 4) if you want to send multiple values in one message, create a model class to contain them, or use a Tuple – Jason Oct 01 '20 at 20:05
  • thank you for your time @Jason atleast im going to finish my small project – Henimex Oct 02 '20 at 05:51
  • Does my solution work for you? If yes, can you please accept it (click the ☑️ in the upper left corner of this answer ) so that we can help more people with same problem:). – nevermore Oct 05 '20 at 08:22
  • already tried to to this but my reputation is not enough. system told "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Henimex Oct 06 '20 at 19:07
  • What you did is vote up.To mark an answer as accepted, click on the check mark(a white ☑️) beside the answer to toggle it from greyed out to filled in. – nevermore Oct 07 '20 at 01:05
  • @Henimex Can you please mark the answer, thanks. – nevermore Oct 08 '20 at 05:35

2 Answers2

0

There is no need to use MessagingCenter in your case, it usually use when publishers send messages without having knowledge of any receivers:

The publish-subscribe pattern is a messaging pattern in which publishers send messages without having knowledge of any receivers, known as subscribers. Similarly, subscribers listen for specific messages, without having knowledge of any publishers.

The quickest way to pass value when you navigation to next page is passing them with the constructor of ConfirmPage:

In InformationPage when you navigate, pass values:

private void Button_Clicked(object sender, EventArgs e)
{
    Navigation.PushAsync(new ConfirmPage(_entry1.Text, _entry2.Text));
}

In ConfirmPage, receive values:

public partial class ConfirmPage : ContentPage
{
    public ConfirmPage()
    {
        InitializeComponent();
    }

    public string value1 { get; set; }
    public string value2 { get; set; }

    public ConfirmPage(string entryOneStr, string entryTwoStr)
    {
        InitializeComponent();

        //get the value 
        value1 = entryOneStr;

        value2 = entryTwoStr;

        //then you can use those values in the ConfirmPage
    }
}
nevermore
  • 15,432
  • 1
  • 12
  • 30
0

=>This link is best to learn how to deal with the messaging center.

=>First, you have to do MessagingCenter.Subscribe after the Subscribe you can use MessagingCenter.Send is working. When you sen a message that message is got in MessagingCenter.Subscribe.

=>In your case no need to use the messaging center.

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

Jasmin Sojitra
  • 1,090
  • 10
  • 24