我正在通过 MessagingCenter 传递一个 id 值。该值从Android Mainactivity传递并在PCL ChatPageviewModel 上订阅。请看下面的代码:
// Sending group Id through the MessagingCenter on android Mainactivity
MessagingCenter.Send<object, string>(this, "webcontentId", webcontentId);
//Subscribing the group id on PCL ChatPageViewmodel
MessagingCenter.Subscribe<object, string>(this, "webcontentId", (object obj, string notifWebcontentId) =>
{
//Checking the id values and refreshing the message feed if ids are equal
});
我的问题是 MessagingCenter.Subscribe 代码执行多次,有时它返回旧的传递 id 值。我认为需要添加MessagingCenter.Unsubscribe
代码来解决这个问题。我在 OnDisappearing 方法上添加了如下代码。
MessagingCenter.Unsubscribe<object, string>(this, "webcontentId");
但是在添加这个之后也会得到旧的组 id 值MessagingCenter.Subscribe
。MessagingCenter.Unsubscribe
我的代码有错误吗?以及在哪里需要添加MessagingCenter.Unsubscribe
代码?