Xamarin消息中心的多个条目



嗨,我试图用消息注册中心发送多个条目,但无法管理它(我是xamarin的新手,找不到合适的代码示例(,我试图在确认页面上识别消息(_entry1你会去这里_entry2你会到那里(

信息页面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"/>

信息页面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");
});

在您的情况下不需要使用MessagingCenter,它通常在发布者不知道任何接收者的情况下发送消息时使用:

发布-订阅模式是一种消息传递模式,其中发布者在不知道任何接收者的情况下发送消息,称为订阅者。类似地,订阅者监听特定的消息,而不知道任何出版商。

导航到下一页时传递值的最快方法是使用ConfirmPage:的构造函数传递值

InformationPage中导航时,传递值:

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

在ConfirmPage中,接收值:

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

=>此链接是学习如何处理消息中心的最佳链接。

=>首先,你必须做MessagingCenter。订阅后,你可以使用MessagingCentre。发送正在工作。当你发送消息时,该消息会在MessagingCenter.Subscribe.中获得

=>在您的情况下,无需使用信息中心。

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

最新更新