接收toast通知时获取当前页面(WP8.1 Silverlight,接收WNS toast通知)
我有一个事件在应用程序处于活动状态时触发,我收到一个通知接收toast通知时获取当前页面(WP8.1 Silverlight,接收WNS toast通知),silverlight,windows-phone-8,windows-phone-8.1,dispatcher,Silverlight,Windows Phone 8,Windows Phone 8.1,Dispatcher,我有一个事件在应用程序处于活动状态时触发,我收到一个通知CurrentChannel\u PushNotificationReceived。在这个函数中,我想找出当前显示的页面,以了解通知是否应该更新页面上的内容。因此,问题有两个:如何知道当前显示的页面以及如何与toast通知交互 更新 问题是,由于与OS线程(Dispatcher)冲突,我无法与元素交互 因此,使用下面的代码,我可以访问消息的内容。但我仍然无法获得当前页面的信息 _channel = await PushNotificat
CurrentChannel\u PushNotificationReceived
。在这个函数中,我想找出当前显示的页面,以了解通知是否应该更新页面上的内容。因此,问题有两个:如何知道当前显示的页面以及如何与toast通知交互
更新
问题是,由于与OS线程(Dispatcher)冲突,我无法与元素交互
因此,使用下面的代码,我可以访问消息的内容。但我仍然无法获得当前页面的信息
_channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;
PushNotificationReceived上的私有void(PushNotificationChannel发送方,PushNotificationReceivedEventArgs参数)
{
开关(参数NotificationType)
{
案例提示类型。徽章:
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;
或
我猜这是因为UI线程。那么我如何使用dispatcher获取信息呢?我已经尝试了一些与dispatcher的解决方案,但我不能等待信息,因此它不适用
System.Windows.Threading.DispatcherOperation op = App.RootFrame.Dispatcher.BeginInvoke(new Func<Uri>(() =>
{
return RootFrame.CurrentSource;
})
);
await op; //Not awaitable.
System.Windows.Threading.dispatcherooperation op=App.RootFrame.Dispatcher.BeginInvoke(新函数(()=>
{
返回RootFrame.CurrentSource;
})
);
等待操作;//不可等待。
好。试试这个。在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属性来识别接收通知时的页面源类型。希望有帮助!确定。尝试此操作。在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
属性来识别接收通知时的页面源类型。希望有帮助!没有理由等待发送程序发送到UI线程。只需发送到UI线程,然后执行其余逻辑,如显示toast或从wi导航到页面精简UI线程
注册事件
var channel=等待PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived+=信道_PushNotificationReceived;
在事件处理程序上,取消显示通知,然后分派到UI线程
private void Channel\u pushnotification已接收(PushNotificationChannel发送方,PushNotificationReceivedEventArgs参数)
{
//确保取消在此线程(而不是UI线程)上显示toast
//因为需要在该线程/方法返回之前设置取消
args.Cancel=true;
//然后分派到UI线程
App.RootFrame.Dispatcher.BeginInvoke(委托
{
var currPage=((PhoneApplicationFrame)Application.Current.RootVisual.Content;
开关(参数NotificationType)
{
案例PushNotificationType.Toast:
//待办事项
打破
}
});
}
在dispatcher的委托内执行所有代码。所有代码都将在UI线程上执行…您将能够导航页面、获取当前页面等。没有理由等待dispatcher发送到UI线程。只需将代码发送到UI线程,然后执行其余逻辑,如显示toast或导航从用户界面线程中转到页面 注册事件
var channel=等待PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived+=信道_PushNotificationReceived;
在事件处理程序上,取消显示通知,然后分派到UI线程
private void Channel\u pushnotification已接收(PushNotificationChannel发送方,PushNotificationReceivedEventArgs参数)
{
//确保取消在此线程(而不是UI线程)上显示toast
//因为需要在该线程/方法返回之前设置取消
args.Cancel=true;
//然后分派到UI线程
App.RootFrame.Dispatcher.BeginInvoke(委托
{
var currPage=((PhoneApplicationFrame)Application.Current.RootVisual.Content;
开关(参数NotificationType)
{
案例PushNotificationType.Toast:
//待办事项
打破
}
});
}
在dispatcher的委托中执行所有代码。所有代码都将在UI线程上执行…您将能够导航页面、获取当前页面等。在项目的app.xaml.cs文件中定义CurrentChannel_PushNotificationReceived方法的位置。事件将绑定在您定义CurrentChann的第一个页面上el_PushNotificationReceived方法?在项目的app.xaml.cs文件中。事件绑定在第一页虽然这是一个解决方案,但它不是一个很好的解决方案。我本来希望不引入静态,并且能够在函数中直接查询信息。但可能这是唯一的方法(?):/你只需要获取页面源类型,它在推送事件中不起作用。因此,我给了你另一个解决方案。使用静态属性会有什么问题?希望你能得到一些好的解决方案。很快:)我可能会开始悬赏,只是为了澄清是否有其他解决方案。如果没有其他人能找到其他解决方案,我会N