Henimex
Henimex

Reputation: 53

Xamarin Multiple Entries With Messaging Center

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");
});

Upvotes: 1

Views: 613

Answers (2)

Jasmin Sojitra
Jasmin Sojitra

Reputation: 1301

=>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

Upvotes: 0

nevermore
nevermore

Reputation: 15786

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
    }
}

Upvotes: 0

Related Questions