在 UWP 中获取 Window 的调度程序
Getting the Dispatcher of a Window in UWP
以下是获取视图调度程序的一些方法。
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher
和
Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher
虽然第一个 returns 主视图的调度器,而后者 returns 活动视图的调度器,
如何获取不活动视图的调度程序?
您需要引用要从中访问 Dispatcher 的视图。如果您创建并以某种方式保存它,请参见下文。或者,您可以通过调用以下命令访问所有视图:
IReadOnlyList<CoreApplicationView> views = CoreApplication.Views;
但是视图没有可直接访问的标识符,因此您需要在视图在调度程序中为它激活后调用以下命令来获取标识符:
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
Frame frame = new Frame();
frame.Navigate(typeof(SecondaryPage), null);
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
然后我建议创建您自己的 IDictionary<int, CoreApplicationView>
以在 ID 和您的视图之间建立映射。或者,您也可以通过
获取 id
newViewId = ApplicationView.GetApplicationViewIdForWindow(newView.CoreWindow);
(进一步documentation)
根据最近的一些经验,我建议不要存储对 Dispatchers 的引用。相反,依靠 page/control 你想要 运行 UI 代码,因为每个 XAML 控件都有自己的 CoreDispatcher。这样,您就可以确定您拥有正确的调度程序,如果它丢失了,则表明出现了问题。代码来自 xaml 来源:
namespace Windows.UI.Xaml
{
//Summary:
//Represents an object that participates in the dependency property system. DependencyObject is the immediate base class of many `enter code here` important UI-related classes, such as UIElement, Geometry, FrameworkTemplate, Style, and ResourceDictionary.
public class DependencyObject : IDependencyObject, IDependencyObject2
{
//Summary:
//Gets the CoreDispatcher that this object is associated with. The CoreDispatcher represents a facility that can access the DependencyObject on the UI thread even if the code is initiated by a non-UI thread.
// Returns: The CoreDispatcher that DependencyObject object is associated with, which represents the UI thread.
public CoreDispatcher Dispatcher { get; }
(...)
}
}
以下是获取视图调度程序的一些方法。
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher
和
Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher
虽然第一个 returns 主视图的调度器,而后者 returns 活动视图的调度器, 如何获取不活动视图的调度程序?
您需要引用要从中访问 Dispatcher 的视图。如果您创建并以某种方式保存它,请参见下文。或者,您可以通过调用以下命令访问所有视图:
IReadOnlyList<CoreApplicationView> views = CoreApplication.Views;
但是视图没有可直接访问的标识符,因此您需要在视图在调度程序中为它激活后调用以下命令来获取标识符:
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
Frame frame = new Frame();
frame.Navigate(typeof(SecondaryPage), null);
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
然后我建议创建您自己的 IDictionary<int, CoreApplicationView>
以在 ID 和您的视图之间建立映射。或者,您也可以通过
newViewId = ApplicationView.GetApplicationViewIdForWindow(newView.CoreWindow);
(进一步documentation)
根据最近的一些经验,我建议不要存储对 Dispatchers 的引用。相反,依靠 page/control 你想要 运行 UI 代码,因为每个 XAML 控件都有自己的 CoreDispatcher。这样,您就可以确定您拥有正确的调度程序,如果它丢失了,则表明出现了问题。代码来自 xaml 来源:
namespace Windows.UI.Xaml
{
//Summary:
//Represents an object that participates in the dependency property system. DependencyObject is the immediate base class of many `enter code here` important UI-related classes, such as UIElement, Geometry, FrameworkTemplate, Style, and ResourceDictionary.
public class DependencyObject : IDependencyObject, IDependencyObject2
{
//Summary:
//Gets the CoreDispatcher that this object is associated with. The CoreDispatcher represents a facility that can access the DependencyObject on the UI thread even if the code is initiated by a non-UI thread.
// Returns: The CoreDispatcher that DependencyObject object is associated with, which represents the UI thread.
public CoreDispatcher Dispatcher { get; }
(...)
}
}