简体   繁体   English

发送带有字符串的广播通知 (UWP)

[英]Send a broadcast notification with string (UWP)

I need to send a notification between two class.我需要在两个班级之间发送通知。 In Android I have used在Android中我用过

Intent intent = new Intent(NOTIFICATION_KEY);
intent.putExtra(ISFIRSTTIME, isfirsttime);     
LocalBroadcastManager.getInstance
(AppDelegate.getContext()).sendBroadcast(intent);

and to recive:并收到:

private BroadcastReceiver multiselectReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

}
};

And in IOS:在 IOS 中:

[[NSNotificationCenter 
defaultCenter]postNotificationName:kNotificationMultiselectController 
object:[NSNumber numberWithBool:isFirstTime]];

and to recive:并收到:

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(multiselectNotification:) 
name:kNotificationMultiselectController object:nil];

There are many solutions for loosely coupled messaging in C#, none UWP-specific.在 C# 中有许多用于松散耦合消息传递的解决方案,没有特定于 UWP 的解决方案。 If you build your app using a MVVM framework, it will likely contain one as well - for example Prism has EventAggregator , MvvmCross has MvxMessenger and in MvvmLight you use MessengerDefault .如果你使用 MVVM 框架构建你的应用程序,它可能也会包含一个 - 例如 Prism 有EventAggregator ,MvvmCross 有MvxMessenger ,在 MvvmLight 中你使用MessengerDefault I would choose one of the frameworks and use the provided messenger capability as it is battle tested and stable.我会选择其中一个框架并使用提供的信使功能,因为它经过了实战测试且稳​​定。 You don't even have to use the full MVVM framework itself and just utilize the event aggregator component.您甚至不必使用完整的 MVVM 框架本身,只需使用事件聚合器组件即可。 For more info on how it works see for example this MSDN blogpost .有关其工作原理的更多信息,请参见此MSDN 博客文章

The most basic solution would be to use basic C# events, but those are strong references, meaning you need to remember to unsubscribe from the events you have registered otherwise the subscriber will stay in memory.最基本的解决方案是使用基本的 C# 事件,但这些是强引用,这意味着您需要记住取消订阅已注册的事件,否则订阅者将留在内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM