1

嗨,我正在尝试使用消息中心发送多个条目,但无法管理它(我是 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);
    }

确认页面 CS

MessagingCenter.Subscribe<InformationPage, string>(this, "EnteryValue", (page, value) =>
{
        _confirm.Text = value;
        MessagingCenter.Unsubscribe<InformationPage, string>(this, "EnteryValue");
});
4

2 回答 2

0

=>这个链接最好学习如何处理消息中心。

=>首先,您必须在订阅后执行 MessagingCenter.Subscribe 您可以使用 MessagingCenter.Send 工作。当您发送消息时,消息会在 MessagingCenter.Subscribe 中获得。

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

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

于 2020-10-02T11:11:21.530 回答
0

在您的情况下不需要使用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
    }
}
于 2020-10-02T06:53:52.357 回答