以下是获取视图调度程序的一些方法。
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher
和
Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()。CoreWindow.Dispatcher
第一个返回主视图的调度程序,后者返回活动视图的调度程序, 如何获取未激活的视图的调度程序?
答案 0 :(得分:2)
您需要引用要从中访问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)
答案 1 :(得分:1)
根据最近的一些经验,我建议不要存储对Dispatchers的引用。相反,依赖于您想要运行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; }
(...)
}
}