首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >应用程序重启时来自Xamarin消息中心的多条消息

应用程序重启时来自Xamarin消息中心的多条消息
EN

Stack Overflow用户
提问于 2016-09-22 20:52:18
回答 4查看 5.2K关注 0票数 4

我有一个Xamarin Forms应用程序,其中我使用MessagingCenter从特定平台向Xamarin.Forms应用程序发送一些数据。

在我的Page中,我用base.OnAppearing()订阅消息,用base.OnDisappearing()方法取消订阅。这可以像预期的那样工作。

我遇到的问题是,当应用程序get被AndroidOS停止时(例如,当我更改设备的语言时),我开始获取消息的副本。我不明白为什么会发生这种情况,但我注意到当应用程序重新启动时,base.OnDisappearing()方法不会被调用。

有没有人知道是什么导致了我的问题,以及如何解决它?另外,有没有办法在Xamarin中看到你所有的发布者和订阅者?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-09-04 17:14:17

这个问题被证明与Xamarin.Forms没有直接关系,而是由于没有在我的MainActivity类的OnStop方法中正确取消注册BroadcastReceiever的问题。因此,每当应用程序经历OnStop/OnStart周期时,即使我只有一个活动订阅,也会发送两条消息。

我还在Xamarin.Forms ContentPages上的OnDisappearing方法中调用了Unsubsribe,并且在调用Subscribe之前还调用了OnAppearing方法中的Unsubscribe

到目前为止,我还没有在任何设备或不同的Android版本上遇到过这个问题。

还有一些额外的建议,不要忘记在OnDisappearing方法中取消订阅任何剩余的MessagingCenter订阅,否则你会遇到一些奇怪的bug,这会让你的头发变白。有一篇关于这个主题的很好的文章,但我找不到了。

票数 0
EN

Stack Overflow用户

发布于 2016-09-22 21:34:24

就像格林西说的,除了在OnAppearing()中订阅和在OnDisappearing()中取消订阅之外,我还会在订阅之前取消订阅消息,因为,为什么不:

protected override async void OnAppearing() {
    base.OnAppearing();

    MessagingCenter.Unsubscribe<string>(this, "KeyHere");
    MessagingCenter.Subscribe<string>(this, "KeyHere", string => { }));
}
票数 3
EN

Stack Overflow用户

发布于 2017-07-03 13:39:41

在Android上的Xamarin.Forms v2.3.4.247中,我们在测试中发现,当模式页面位于导航页面的顶部时,当您离开应用程序时,导航页面将调用其OnDisappearing()方法,而不是实际位于顶部的模式页面。为了解决这个问题,我们做了以下工作:-在OnAppearing方法中,检查你是否真的在上面-如果你在上面,一切都好-如果你不在上面,在你自己调用SendDisappearing(),在上面的实际页面上调用SendAppearing()。我们还发现需要在顶部的页面上调用SendDisappearing(),因为Xamarin forms可以保护这些方法不被多次调用。换句话说,如果调用SendDisappearing()两次,它只会做一次。

下面是一个适用于我们的示例,因为我们有一个BaseContentPage,所有页面都是从这个are派生出来的:

protected override void OnAppearing()
{
    base.OnAppearing();
    var pageOnTop = ModalContentPageOnTop();
    if (pageOnTop == null || pageOnTop == this)
    {
        // do some stuff, like setting up subscriptions
        SetupSubscriptions();
    }
    else
    {
        // Xamarin Forms Appearing/Disappearing Bug
        // Found that when you leave the Android app with a modal page on top of a non-modal page (either minimise the app or lock the device), then Xamarin.Forms does not properly call OnAppearing. 
        // Xamarin.Forms will call OnAppearing on the non-modal page and not on the modal page. This would not only cause message subscriptions to not be set up on the modal page, 
        // but would also set up message subscriptions on the non-modal page. Not good.  Hopefully, this work-around should stop being executed when Xamarin fixes the problem, as we won't 
        // fall into this "else" condition above, because OnAppearing will be called on the right page.

        // NOTE: SendDisappearing and SendAppearing have boolean guards on them. If SendAppearing was called last, calling it again will do nothing.
        // SendDisappearing has the same behaviour. Thus we need to call SendDisappearing first, to be guaranteed that SendAppearing will cause the OnAppearing() method to execute.
        ((IPageController)pageOnTop).SendDisappearing();  // try and correct Xamarin.Forms - the page actually on top was not told it was disappearing - tell it now
        ((IPageController)this).SendDisappearing();  // try and correct Xamarin.Forms - if it thinks the view has appeared when it hasn't, it won't call OnAppearing when it is truly appearing.
        ((IPageController)pageOnTop).SendAppearing();  // try and correct Xamarin.Forms by notifying the correct page that it is appearing
    }
}

其中ModalContentPageOnTop看起来像:

private ContentPage ModalContentPageOnTop()
{
    var page = Navigation.ModalStack.LastOrDefault();
    var navigationPage = page as NavigationPage;
    if (navigationPage != null)
        return navigationPage.CurrentPage as ContentPage;
    return page as ContentPage;
}

我们的应用程序中的导航层次非常浅,因此这种方法可能不适用于更复杂的导航堆栈(即推送其他页面或其他模式页面的模式页面)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39639619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档