【发布时间】:2016-02-17 15:26:42
【问题描述】:
我有一个在应用程序上线时触发的事件,我收到通知 CurrentChannel_PushNotificationReceived。在此功能中,我想找出当前显示的页面,以了解通知是否应该更新页面上的内容。因此,问题是双重的,如何知道当前显示的是哪个页面并与 toast 通知交互。
更新 问题是由于与操作系统线程(调度程序)发生冲突,我无法与元素交互。
因此,使用下面的代码可以让我访问消息的内容。但是我仍然无法获取 current_page 的信息
_channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;
私人无效 OnPushNotificationReceived(PushNotificationChannel 发送者,PushNotificationReceivedEventArgs 参数) { 开关(args.NotificationType) { 案例 PushNotificationType.Badge: this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml()); 休息;
case PushNotificationType.Tile:
this.OnTileNotificationReceived(args.TileNotification.Content.GetXml());
break;
case PushNotificationType.Toast:
this.OnToastNotificationReceived(args.ToastNotification.Content.GetXml());
break;
case PushNotificationType.Raw:
this.OnRawNotificationReceived(args.RawNotification.Content);
break;
}
args.Cancel = true;
}
private void OnBadgeNotificationReceived(string notificationContent)
{
// Code when a badge notification is received when app is running
}
private void OnTileNotificationReceived(string notificationContent)
{
// Code when a tile notification is received when app is running
}
private void OnToastNotificationReceived(string notificationContent)
{
// Code when a toast notification is received when app is running
// Show a toast notification programatically
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(notificationContent);
var toastNotification = new ToastNotification(xmlDocument);
//toastNotification.SuppressPopup = true;
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}
private void OnRawNotificationReceived(string notificationContent)
{
// Code when a raw notification is received when app is running
}
问题
如何访问不同onXXXXNotificationReceived 中的当前页面信息。当前的 sn-ps 工作但不在这些功能内:
var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
var tempBool = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
或
RootFrame.CurrentSource;
我的猜测是因为 UI 线程。那么如何使用调度程序来获取信息呢?我已经尝试了调度员的一些解决方案,但我无法等待信息,因此它不适用。
System.Windows.Threading.DispatcherOperation op = App.RootFrame.Dispatcher.BeginInvoke(new Func<Uri>(() =>
{
return RootFrame.CurrentSource;
})
);
await op; //Not awaitable.
【问题讨论】:
-
你在哪里定义了 CurrentChannel_PushNotificationReceived 方法?
-
在项目的 app.xaml.cs 文件中。事件绑定在第一页
标签: silverlight windows-phone-8 windows-phone-8.1 dispatcher