MessageCenter 订阅方法不会发生
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");
});
我以前从未使用过消息中心,在应用程序之间传递消息时可能会犯一些荒谬的错误。我尝试将消息从 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");
});