【问题标题】:MessagingCenter is working on WPF but not working on AndroidMessagingCenter 在 WPF 上工作,但在 Android 上不工作
【发布时间】:2019-10-16 10:58:54
【问题描述】:

我创建这个应用程序是为了回答一些有关 MessagingCenter 的问题,但由于专门在 Android 平台上运行应用程序时出现问题,我无法继续编写代码,如果您知道可能出了什么问题,请帮助我。感谢您的支持。

我尝试将结果页面更改为消息中心订阅中新结果视图的方式,但我不知道发生了什么,对我来说就像在订阅中找不到消息一样。

应用链接(GitHub)

在结果视图中:

public void Registro()
{
    MessagingCenter.Subscribe<ResultView>(this, "DisplayAlert", message =>
    {
        this.DisplayAlert("Alerta de Registro", "Mensagem DisplayAlert com registro Enviada", "Ok");
    });
}

在主页中:

ResultView ResultPage = new ResultView();    

private void GoPaginaResultComRegistro(object sender, EventArgs e)
{
    ResultPage.Registro();
    MessagingCenter.Send<ResultView>(ResultPage, "DisplayAlert");
    MessagingCenter.Unsubscribe<ResultView>(ResultPage, "DisplayAlert");
    this.Navigation.PushAsync(ResultPage);
}

我在发送消息时在另一个屏幕上等待 DisplayAlert,但应用程序只是跳过了订阅内的代码。

【问题讨论】:

  • 这似乎是对 MessagingCenter 的复杂使用。如果您已经有一个 ResultPage 实例并且即将导航到它,为什么不直接在构造函数上传递一个参数,或者使用公共属性/方法?
  • 该应用程序的目的是练习使用 MessagingCenter,但如果您能找到另一种简单的方法来完成相同的任务,如果您能告诉我如何操作,我将不胜感激。

标签: xamarin.forms messagingcenter


【解决方案1】:

首先在你的GoPaginaResultComRegistro()方法中,你应该在PushAsync之后发送消息

private void GoPaginaResultComRegistro(object sender, EventArgs e)
    {
        ResultPage.Registro();
        this.Navigation.PushAsync(ResultPage);
        MessagingCenter.Send<ResultView>(ResultPage, "DisplayAlert");
        MessagingCenter.Unsubscribe<ResultView>(ResultPage, "DisplayAlert");

    }

第二在您的ResultView 页面中,在MainThread 中调用DisplayAlert

 public void Registro()
    {
        MessagingCenter.Subscribe<ResultView>(this, "DisplayAlert", message =>
        {
            Device.BeginInvokeOnMainThread( async() =>
            {
                await DisplayAlert("Alerta de Registro", "Mensage DisplayAlert com registro Enviada", "Ok");
            });

        });
    }

【讨论】:

  • 我现在不工作了,谢谢,你能解释一下发生了什么吗?
  • @MATCH 首先,DisplayAlert 它依赖于 NavigationStack 中存在的页面,所以你应该先 pushAsync 页面,其次 DisplaAlert 应该在 UI 线程中调用,所以你应该使用 Device.BeginInvokeOnMainThread ,如果可行,请您标记并投票,谢谢
  • 我不能投票,我需要更多的声望点,但我会尽快回到这里并留下我的投票。再次感谢您。
【解决方案2】:

试试这个

 public void Registro()
        {
            MessagingCenter.Subscribe<ResultView,string>(this, "DisplayAlert", async (sender,message) =>
            {
                await DisplayAlert("Alerta de Registro", message, "Ok");
            });
    }

var mensagem = "teste";
MessagingCenter.Send<ResultView,string>(ResultPage, "DisplayAlert",mensagem);

这是我在项目中使用的一些示例

在我的 PCL MainPage.cs 中

public MainPage()
        {   
            InitializeComponent();
            MessagingCenter.Send<string>("ok", "showBar");

        }

在我的 Native android 项目 MainActivity.cs 中

protected override void OnCreate(Bundle savedInstanceState)
        {
            MessagingCenter.Subscribe<string>(this, "showBar", (sender) =>
            {

                this.Window.ClearFlags(WindowManagerFlags.Fullscreen);

            });
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            this.Window.AddFlags(WindowManagerFlags.Fullscreen);
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

您无需创建页面的新实例即可作为参数发送。

【讨论】:

  • 如果您需要更多帮助,可以在 whatsapp (11948659453) 中给我发短信,我也是巴西人。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多