2016-12-07 3 views
0

다음은보기의 디스패처를 가져 오는 몇 가지 방법입니다.UWP에서 창 Dispatcher 가져 오기

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

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

첫 번째 것은 메인 뷰의 디스패처를 반환하고 후자는 활성 뷰의 디스패처를 반환하지만 보기, 활성화되지 않은보기의 디스패처를 어떻게 얻을 수 있습니까?

답변

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; 
}); 

은 그럼 ID와 귀하의 의견 사이의 매핑을 가지고 IDictionary<int, CoreApplicationView> 직접 만들 건의 할 것입니다. 또는 당신은 또한 최근의 경험을 한 후

newViewId = ApplicationView.GetApplicationViewIdForWindow(newView.CoreWindow); 

(일부 더 documentation)

1

하여 ID를 얻을 수있다, 나는 디스패처에 대한 참조를 저장에 대해 건의 할 것입니다. 대신 모든 XAML 컨트롤에는 자체 CoreDispatcher가 있으므로 UI ​​코드를 실행할 페이지/컨트롤을 사용하십시오. 그렇게하면 올바른 발송자를 확보 할 수 있고 누락 된 경우 문제가 있다는 신호가됩니다. 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; } 

(...) 

} 
}