2016-12-07 69 views
0

以下是一些獲取視圖調度程序的方法。獲取UWP窗口的調度程序

Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher

Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()。CoreWindow.Dispatcher

雖然第一個返回mainview的調度程序,而後者返回活動的調度程序查看, 如何獲取不活動的視圖的調度程序?

回答

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

獲得ID之後最近的一些經驗,我建議對存儲到調度員引用。相反,依賴於您想要運行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; } 

(...) 

} 
}