【发布时间】:2019-03-09 22:21:45
【问题描述】:
在我的 UWP 应用中,当 post call 成功时,我需要向用户显示通知。此通知必须显示在用户所在的当前窗口中。我只会在后台线程中了解 post call 的成功。因此,一旦我从服务器收到成功代码,我需要确定哪个是当前窗口,并且必须在该窗口中向用户显示通知。
Window.Current 从后台线程访问时给我 null。
如果我尝试在CoreApplication.MainView.Dispatcher.RunAsync 中访问Window.Current,它会给我主窗口。
如果我尝试从后台线程访问CoreApplication.GetCurrentView().CoreWindow.DispatcherCoreApplication.GetCurrentView() 会抛出以下异常
Exception = {System.Runtime.InteropServices.COMException (0x80070490): Element not found.
Element not found.
at Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()
我做了一小轮分析,以了解以下代码 sn-p 发生了什么。
foreach (var iteratingview in CoreApplication.Views)
{
iteratingview.Dispatcher.RunOnUIThread(() =>
{
int id = ApplicationView.GetForCurrentView().Id;
bool main = CoreApplication.GetCurrentView().IsMain;
bool isMain = iteratingview.IsMain;
});
}
当iteratingview 是主视图时,布尔值CoreApplication.GetCurrentView().IsMain 给我true,当iteratingview 是辅助视图时,false 给我,这意味着CoreApplication.GetCurrentView() 给我返回我在哪个调度程序里面的视图我叫它。 ApplicationView.GetForCurrentView() 的行为方式也相同。
但我的要求是确定用户所在的当前窗口/视图。
是我做错了什么,还是有其他方法可以从后台线程获取当前窗口的调度程序?
【问题讨论】: