收到toast通知时获取当前页面(WP8.1 Silverlight,收到WNS toast通知)
Getting the current page when receiving a toast notification (WP8.1 Silverlight, receiving WNS toast notification)
我有一个事件会在应用程序运行时触发,并且我会收到通知 CurrentChannel_PushNotificationReceived
。在此功能中,我想找出当前显示的页面以了解通知是否应更新页面上的内容。因此问题是双重的,如何知道当前显示的是哪个页面并与toast通知进行交互。
更新
问题是由于与 OS 线程(调度程序)发生冲突,我无法与元素交互。
因此使用下面的代码可以让我访问消息的内容。但是我仍然无法获取 current_page
的信息
_channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;
private void OnPushNotificationReceived(PushNotificationChannel 发件人,PushNotificationReceivedEventArgs args)
{
开关(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
中的当前页面信息。当前的代码片段有效但不适用于这些函数:
var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
var tempBool = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
或
RootFrame.CurrentSource;
我猜是因为 UI-thread。那么如何使用调度程序获取信息呢?我已经用调度器尝试了一些解决方案,但我不能等待信息,因此它不适用。
System.Windows.Threading.DispatcherOperation op = App.RootFrame.Dispatcher.BeginInvoke(new Func<Uri>(() =>
{
return RootFrame.CurrentSource;
})
);
await op; //Not awaitable.
好的。试试这个。在 App.xaml.cs.
上创建静态 属性
public static object CurrentPageInfo { get; set; }
并将页面类型或页面名称分配给每个页面上的 属性 on 'OnNavigatedTo' 方法。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
App.CurrentPageInfo = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
}
以便您可以通过访问 App.CurrentPageInfo
属性 来识别接收通知的页面源类型。希望对您有所帮助!
没有理由等待 UI 线程的调度程序。只需分派到 UI 线程,然后从 UI 线程中执行其余逻辑,例如显示吐司或导航到页面...
注册活动...
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += Channel_PushNotificationReceived;
在事件处理程序上,取消显示通知,然后分派到 UI 个线程...
private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
// Make sure you cancel displaying the toast on this thread (not on UI thread)
// since cancellation needs to be set before this thread/method returns
args.Cancel = true;
// Then dispatch to the UI thread
App.RootFrame.Dispatcher.BeginInvoke(delegate
{
var currPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
switch (args.NotificationType)
{
case PushNotificationType.Toast:
// TODO
break;
}
});
}
在调度员的委托中执行所有代码。您的所有代码都将在 UI 线程上执行...您将能够导航页面、获取当前页面等。
我有一个事件会在应用程序运行时触发,并且我会收到通知 CurrentChannel_PushNotificationReceived
。在此功能中,我想找出当前显示的页面以了解通知是否应更新页面上的内容。因此问题是双重的,如何知道当前显示的是哪个页面并与toast通知进行交互。
更新 问题是由于与 OS 线程(调度程序)发生冲突,我无法与元素交互。
因此使用下面的代码可以让我访问消息的内容。但是我仍然无法获取 current_page
的信息 _channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;
private void OnPushNotificationReceived(PushNotificationChannel 发件人,PushNotificationReceivedEventArgs args) { 开关(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
中的当前页面信息。当前的代码片段有效但不适用于这些函数:
var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
var tempBool = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
或
RootFrame.CurrentSource;
我猜是因为 UI-thread。那么如何使用调度程序获取信息呢?我已经用调度器尝试了一些解决方案,但我不能等待信息,因此它不适用。
System.Windows.Threading.DispatcherOperation op = App.RootFrame.Dispatcher.BeginInvoke(new Func<Uri>(() =>
{
return RootFrame.CurrentSource;
})
);
await op; //Not awaitable.
好的。试试这个。在 App.xaml.cs.
上创建静态 属性public static object CurrentPageInfo { get; set; }
并将页面类型或页面名称分配给每个页面上的 属性 on 'OnNavigatedTo' 方法。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
App.CurrentPageInfo = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
}
以便您可以通过访问 App.CurrentPageInfo
属性 来识别接收通知的页面源类型。希望对您有所帮助!
没有理由等待 UI 线程的调度程序。只需分派到 UI 线程,然后从 UI 线程中执行其余逻辑,例如显示吐司或导航到页面...
注册活动...
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += Channel_PushNotificationReceived;
在事件处理程序上,取消显示通知,然后分派到 UI 个线程...
private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
// Make sure you cancel displaying the toast on this thread (not on UI thread)
// since cancellation needs to be set before this thread/method returns
args.Cancel = true;
// Then dispatch to the UI thread
App.RootFrame.Dispatcher.BeginInvoke(delegate
{
var currPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
switch (args.NotificationType)
{
case PushNotificationType.Toast:
// TODO
break;
}
});
}
在调度员的委托中执行所有代码。您的所有代码都将在 UI 线程上执行...您将能够导航页面、获取当前页面等。