[英]Send a broadcast notification with string (UWP)
我需要在兩個班級之間發送通知。 在Android中我用過
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# 事件,但這些是強引用,這意味着您需要記住取消訂閱已注冊的事件,否則訂閱者將留在內存中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.