簡體   English   中英

帶有消息中心的 Xamarin 多個條目

[英]Xamarin Multiple Entries With Messaging Center

嗨,我正在嘗試使用消息中心發送多個條目,但無法管理它(我是 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");
});

在您的情況下不需要使用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.Subscribe,您可以使用 MessagingCenter.Send 工作。 當您發送消息時,該消息是在 MessagingCenter.Subscribe 中獲得的。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM