簡體   English   中英

消息中心訂閱方法沒有發生

[英]MessageCenter subscription method does not happen

我以前從未使用過消息中心,在應用程序之間傳遞消息時可能會犯一些可笑的錯誤。 我嘗試從 Xamarin.Android 向 Xamarin.Forms 發送消息。

Xamarin.Android 項目代碼:

MessagingCenter.Send(Xamarin.Forms.Application.Current, "MessageReceived", "new message");

Xamarin.Forms:

public partial class App : Application
{

    public App()
    {

        InitializeComponent();

        MainPage = (new MasterDetailPage()
        {
            Master = new MenuPage { Title = "Title" },
            Detail = new NavigationPage(new XxmsApp.MainPage()) { BarBackgroundColor = Color.Black }
        });

        MessagingCenter.Subscribe<App>(
            this,                                                     
            "MessageReceived",        
            (sender) => 
            {
                var StartPage = (((MainPage as MasterDetailPage).Detail as NavigationPage).RootPage as XxmsApp.MainPage);

                StartPage.DisplayAlert(
                    "message",
                    "message received",
                    "ok");
            });    
    }

    // ...

}

但是永遠不會調用指定為Subscribe<App>第三個參數的方法。 我也試過這個:

        Device.BeginInvokeOnMainThread(() =>
        {
            MessagingCenter.Send(Xamarin.Forms.Application.Current, "MessageReceived", "new message");
        });

但沒什么。

我究竟做錯了什么?

在您的 android 項目代碼中

MessagingCenter.Send<object, object>(this, "MessageReceived", "new message");

在您的共享代碼中

 MessagingCenter.Subscribe<object, object>(this, "MessageReceived", (sender, args) =>
    {

var StartPage = (((MainPage as MasterDetailPage).Detail as NavigationPage).RootPage as XxmsApp.MainPage);

                StartPage.DisplayAlert(
                    "message",
                    "message received",
                    "ok");
    });

暫無
暫無

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

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