Xamarin表格MessagingCenter退订无法正常工作 [英] Xamarin form MessagingCenter Unsubscribe is not working as expected

查看:225
本文介绍了Xamarin表格MessagingCenter退订无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在应用程序中来回浏览多次时,多次调用MessagingCenter.Subscribe()内部编写的功能.但是每次订阅之前,我都会按照以下方式取消订阅构造函数中的相同对象,但仍然没有用.

Functionality written inside the MessagingCenter.Subscribe() is called multiple times when i navigate to and fro multiple times in the application. But each time before subscribing, i do unsubscribe to the same in constructor as follows, still it didn't worked.

MessagingCenter.Unsubscribe<SubmitPage>(this,"Save");
MessagingCenter.Subscribe<SubmitPage>(this, "Save", (sender) =>
{
   DisplayToastOnSuccessfulSubmission();
});

在我的应用程序中,我有6页( git ),并使用MessagingCenter将数据保存在第6页中发送和相同的内容将在第二页中进行订阅,保存的消息将显示在第二页中(导航到该页面后).

In my application i have 6 pages(git) and i save the data in 6th page with MessagingCenter.Send and same will be subscribed in 2nd page and saved message will be displayed in 2nd page(after navigating to that page).

现在,在这种特殊情况下,我将像2-> 1-> 2-> 3-> 4-> 5-> 6那样导航(两次调用DisplayToastOnSuccessfulSubmission())(因为Page2构造函数被调用了两次).

Now i navigate like 2->1->2->3->4->5->6 in this particular case DisplayToastOnSuccessfulSubmission() would be called two times(because Page2 constructor is called twice).

我什至尝试将相同的代码放在OnAppearing中. 我无法取消订阅OnDisappear,因为当我到达第6页进行保存时,需要连接事件.

I even tried placing the same code in OnAppearing. I can't unsubscribe in OnDisappear as I need the event wiring up to when I reach Page6 for save.

在示例项目中复制了相同的行为,并在此处添加了 https://github.com/suchithm/MessageCenterSampleApp
拖放框链接

Reproduced the same behaviour in sample project and added here https://github.com/suchithm/MessageCenterSampleApp
Drop box link

执行此操作的正确方法是什么?

What is the proper way to do this?

推荐答案

但是每次订阅之前,我都会按照以下方式取消订阅构造函数中的相同内容,但仍然没有用.

But each time before subscribing, I do unsubscribe to the same in constructor as follows, still it didn't worked.

MessagingCenter.Subscribe()被多次调用,因为在代码中有两个Page2实例,它们都使用MessagingCenter.Subscribe()方法,这就是Unsubscribe不起作用的原因.

MessagingCenter.Subscribe() is called multiple times, because there are two instances of Page2 in your code, both of them use MessagingCenter.Subscribe() method, that's why the Unsubscribe didn't work.

您可以将page2()修改为 singleton ,以确保项目中只有一个Page2实例,然后在发送消息时, MessagingCenter.Subscribe()仅被调用一次.

You can modify page2() to a singleton to make sure there is only one instance of Page2 in your project, after that when you send a message, the MessagingCenter.Subscribe() is called only once.

Page2.cs:

public static Page2 instance = new Page2();

public static Page2 GetPage2Instance()
{
    if(instance == null)
    {
        return new Page2();
    }
    return instance;
}

private Page2()
{
    InitializeComponent();
    MessagingCenter.Unsubscribe<Page2>(this, "SaveToastPage2");
    MessagingCenter.Subscribe<Page2>(this, "SaveToastPage2", (sender) =>
    {
       DisplayToastOnSuccessfulSubmission();
    }
 }

发送消息时:

MessagingCenter.Send(Page2.GetPage2Instance(), "SaveToastPage2");

编辑:

请记住,将Page2类的构造函数声明为私有的,以确保项目中仅存在Page2的一个实例.

Remember that declaring constructors of Page2 class to be private to make sure there is only one instance of Page2 in your project sure.

private Page2()
{
   ...
}

修改您的Page1.cs代码:

async void Handle_Next(object sender, System.EventArgs e)
{
    await App.NavigationRef.PushAsync(Page2.GetPage2Instance(), true);
}

这篇关于Xamarin表格MessagingCenter退订无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆