使用 MessagingCenter 从 Android MainActivity 发送消息以查看不在 Android 项目中的模型类时遇到问题 [英] Having trouble using MessagingCenter to send a message from Android MainActivity to view model class not in Android project

查看:86
本文介绍了使用 MessagingCenter 从 Android MainActivity 发送消息以查看不在 Android 项目中的模型类时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照此处的说明操作在将打开特定视图的通知点击上添加 MessagingCenter 订阅操作.某处我的发送/订阅没有相互交谈,我只是看不到在哪里.消息中心的细节对我来说仍然很陌生,所以我确定我只是在某处使用了错误的类或发件人.

I attempted to follow the instructions found here to add a MessagingCenter subscribe action on a notification tap that would open a specific view. Somewhere my Send / Subscribe aren't talking to each other and I just can't see where. The details of the messaging center are still new to me so I'm sure I just used the wrong class or sender somewhere.

以下代码已根据链接中显示的内容进行了修改.不过,这个想法仍然大致相同.

The code below has since been modified from what was shown to me in the link. The idea is still roughly the same though.

这是我的 FirebaseService 类中的 SendLocalNotification 方法:

Here's the SendLocalNotification method in my FirebaseService class:

void SendLocalNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.SingleTop);
        intent.PutExtra("OpenPage", "SomePage");

        //Unique request code to avoid PendingIntent collision.
        var requestCode = new Random().Next();
        var pendingIntent = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetContentTitle("Load Match")
            .SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetShowWhen(false)
            .SetContentIntent(pendingIntent);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
        }

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }

这是androidMainActivity中的OnNewIntent方法:

Here's the OnNewIntent method in the android MainActivity:

protected override void OnNewIntent(Intent intent)
    {
        if (intent.HasExtra("OpenPage"))
        {
            MessagingCenter.Send(this, "Notification");
        }

        base.OnNewIntent(intent);
    }

这里是我尝试在我的 LoadsPageViewModel 中订阅消息的地方(不是在 android 项目中):

And here is where I attempt to subscribe to the message in my LoadsPageViewModel (not in the android project):

public LoadsPageViewModel()
    {
        MessagingCenter.Subscribe<LoadsPageViewModel>(this, "Notification", (sender) =>
        {
             // navigate to a view here
        });
    }

推荐答案

要使 MessagingCenter 正常工作,您需要在发送方和订阅方使用相同的类型/对象.

For the MessagingCenter to work, you need to use the same type/object on the sender and the subscriber.

由于您是从 Android 项目发送的,因此您在此处使用的 this 值:

Since you're sending from the Android Project, the this value you are using here:

MessagingCenter.Send(this, "Notification");

代表 MainActivity.

represents the MainActivity.

当您订阅 ViewModel 时,您正在使用 ViewModel 对象

And when you are subscribing in your ViewModel you are using the ViewModel object

MessagingCenter.Subscribe<LoadsPageViewModel>(this, "Notification", (sender) => { });

这就是你没有收到对方消息的原因.

This is the reason why you are not receiving the Message on the other side.

要使其工作,您需要更改以下内容:

To make it work you will need to change the following:

在 Android Main Activity 中,使用 Xamarin.Forms.Application 类:

In the Android Main Activity, use the Xamarin.Forms.Application Class:

MessagingCenter.Send(Xamarin.Forms.Application.Current, "Notification");

并且在您的 ViewModel 中使用相同的 Xamarin.Forms.Application 类和对象:

And in your ViewModel use the same Xamarin.Forms.Application class and object:

MessagingCenter.Subscribe<Xamarin.Forms.Application>(Xamarin.Forms.Application.Current, "Notification", (sender) =>
{
    Console.WriteLine("Received Notification...");
});

这样你就会遵守MessagagingCenter 的期望.

That way you will comply with what MessagagingCenter is expecting.

希望这会有所帮助.-

这篇关于使用 MessagingCenter 从 Android MainActivity 发送消息以查看不在 Android 项目中的模型类时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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