我有一个活动,当应用程序处于活动状态时会触发,我会收到通知gulp compileUnit --page about
。在此功能中,我想找出当前显示的页面,以了解通知是否应更新页面上的内容。因此,问题是双重的,如何知道当前显示哪个页面并与Toast通知进行交互。
更新 问题是由于与OS线程(Dispatcher)的冲突,我无法与元素交互。
因此,使用下面的代码,它允许我访问消息的内容。但我仍然无法获得current_page的信息
CurrentChannel_PushNotificationReceived
private void OnPushNotificationReceived(PushNotificationChannel sender,PushNotificationReceivedEventArgs args) { switch(args.NotificationType) { case PushNotificationType.Badge: this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml()); 打破;
_channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;
问题
如何访问不同 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;
我的猜测是因为UI线程。那么如何使用调度程序获取信息呢?我已经向调度员尝试了一些解决方案,但我不能等待这些信息,因此不适用。
RootFrame.CurrentSource;
答案 0 :(得分:3)
没有理由等待调度程序到UI线程。只需调度到UI线程,然后执行其余逻辑,例如在UI线程中显示toast或导航到页面......
注册活动......
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线程上执行...您将能够导航页面,获取当前页面等。
答案 1 :(得分:1)
确定。试试这个。在App.xaml.cs上创建一个静态属性。
public static object CurrentPageInfo { get; set; }
并将页面类型或页面名称分配给' 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
属性,您可以在接收通知时识别页面源类型。希望它有所帮助!