标签: messagingcenter

使用 MessagingCenter 和标准 .NET 事件处理程序将更改通知相关方有什么区别?

使用 MessagingCenter 和标准 .NET 事件处理程序将更改通知相关方有什么区别?

下面是同一事物的两个(未经测试的)实现来演示:

public class FooClass {
  public event EventHandler SomeEvent;

  public void DoSomeWork() {
     // ... stuff
    if(SomeEvent != null)
      SomeEvent(this, EventArgs.Empty);
  }
}

public class BarClass {
  FooClass _foo;

  public BarClass() {
    _foo = new FooClass();
    _foo.SomeEvent += delegate {
      // ... did something
   };
  }
}
Run Code Online (Sandbox Code Playgroud)

诗句:

public class FooClass {
  public const string SomeEventName = "SomeEvent";
  public void DoSomeWork() {
    // ... stuff
    MessagingCenter.Send<FooClass>(this, SomeEventName);
  }
}

public class BarClass : IDisposable { …
Run Code Online (Sandbox Code Playgroud)

.net c# events xamarin.forms messagingcenter

5
推荐指数
2
解决办法
1995
查看次数

使用 MessagingCenter 从 Android MainActivity 发送消息以查看不在 Android 项目中的模型类时遇到问题

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

下面的代码已根据链接中向我显示的内容进行了修改。但这个想法仍然大致相同。

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

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());
    }
Run Code Online (Sandbox Code Playgroud)

这是android MainActivity中的OnNewIntent方法:

protected override void OnNewIntent(Intent intent)
    {
        if (intent.HasExtra("OpenPage")) …
Run Code Online (Sandbox Code Playgroud)

c# android push-notification xamarin.forms messagingcenter

1
推荐指数
1
解决办法
2398
查看次数