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



我需要在两个类之间发送通知。 在安卓中,我用过

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

并收到:

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

在 IOS 中:

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

并收到:

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

C# 中松散耦合的消息传递有许多解决方案,没有特定于 UWP 的解决方案。如果您使用 MVVM 框架构建应用程序,它可能也会包含一个 - 例如 Prism 有EventAggregator,MvvmCross 有MvxMessenger,在 MvvmLight 中使用MessengerDefault。我会选择其中一个框架并使用提供的信使功能,因为它经过实战测试和稳定。您甚至不必使用完整的 MVVM 框架本身,只需使用事件聚合器组件即可。有关其工作原理的详细信息,请参阅此 MSDN 博客文章。

最基本的解决方案是使用基本的 C# 事件,但这些是强引用,这意味着您需要记住取消订阅已注册的事件,否则订阅者将保留在内存中。

最新更新