WinRT提供了一个Windows.ApplicationModel.Core.CoreApplication类:
public static class CoreApplication {
// Returns the CoreApplicationView associated with the calling thread
public static CoreApplicationView GetCurrentView();
// Returns all CoreApplicationViews existing within the process
public static IReadOnlyList<CoreApplicationView> Views { get; }
// Returns the main view thread's CoreApplicationView
public static CoreApplicationView MainView { get; }
// These events are discussed later in this chapter
public static event EventHandler<Object> Resuming;
public static event EventHandler<SuspendingEventArgs> Suspending;
// These events are for debugging only
public static event EventHandler<Object> Exiting;
public static event EventHandler<UnhandledErrorDetectedEventArgs> UnhandledErrorDetected;
// This method allows you to create multiple main view windows
public static CoreApplicationView CreateNewView();
// Some members not shown here...
}
此静态类管理整个app。但是静态类破坏了面向对象,所以微软用单例的Application对象封装了此CoreApplication类。
public class Application {
// Static members:
public static void Start(ApplicationInitializationCallback callback);
public static Application Current { get; }
// The same Resuming & Suspending events offered by the CoreApplication class
public event EventHandler<object> Resuming;
public event SuspendingEventHandler Suspending;
// XAML-specific properties and events:
public DebugSettings DebugSettings { get; }
public ApplicationTheme RequestedTheme { get; set; }
public ResourceDictionary Resources { get; set; }
public event UnhandledExceptionEventHandler UnhandledException;
// The virtual methods shown earlier and some other members are not shown here...
}
App类派生自Application类,继承了所有成员。
CoreApplication类有很多成员都返回CoreApplicationView对象。而CoreApplicationView类有这些成员:
public sealed class CoreApplicationView {
public CoreDispatcher Dispatcher { get; }
public CoreWindow CoreWindow { get; }
public Boolean IsMain { get; }
public Boolean IsHosted { get; }
public event TypedEventHandler<CoreApplicationView, IActivatedEventArgs> Activated;
}
注意有一个CoreDispatcher(分发Windows消息)和一个CoreWindow(主绘制窗口),另外还有标识激活方式的属性。
CoreWindow中的字段标识了矩形窗口大小,以及是否可以交互,显示光标,是否可见等。还有Activated,Close,SizeChanged等事件。
有一个Window类轻度封装了这个CoreWindow对象:
public sealed class Window {
public static Window Current { get; } // Returns calling thread's Window
public CoreWindow CoreWindow { get; }
public CoreDispatcher Dispatcher { get; } // Same as CoreApplicationView.Dispatcher
// The Content property is how XAML integrates with the window's drawing surface:
public UIElement Content { get; set; }
// This class exposes some of the same properties (Bounds, Visible)
// This class exposes some of the same events (Activated, Closed,
// SizeChanged, VisibilityChanged)
// This class exposes some of the same methods (Activate, Close)
}
CoreDispatcher类是这样的:
public sealed class CoreDispatcher {
// Returns true if the calling thread is the same thread
// that this CoreDispatcher object is associated with
public Boolean HasThreadAccess { get; }
// Call this to have the CoreDispatcher's thread execute the agileCallback
// with a priority of Idle, Low, Normal, or High
public IAsyncAction RunAsync(
CoreDispatcherPriority priority, DispatchedHandler agileCallback);
// Call this to get/set the priority of the code that dispatcher is currently executing
public CoreDispatcherPriority CurrentPriority { get; set; }
// Other members not shown...
}
当非UI线程想更新UI时使用。
CoreApplication的静态方法CreateNewView可以创建新的视图和线程,用于显示更多内容。新的线程会附着于原CoreWindow,并返回CoreApplicationView。而CoreApplicationView的IsMain和IsHosted属性都返回false。另外,OnWindowCreated也属于新的线程。可以按照下面的方式创建新的视图:
private async Task CreateNewViewWindow() {
// Have Windows create a new thread, CoreDispatcher, CoreWindow, and CoreApplicationView
CoreApplicationView cav = CoreApplication.CreateNewView();
CoreWindow newAppViewWindow = null; // This will hold the new view's window
// Have the new thread initialize the new view's content
await cav.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
// Give the new thread's window back to the creating thread
newAppViewWindow = Window.Current.CoreWindow;
// Create the desired UI element tree and make it the content of the new window
Window.Current.Content = new MyPage();
Window.Current.Activate();
});
// After the new thread initializes its view, the creating thread makes it appear
Int32 newAppViewId = ApplicationView.GetApplicationViewIdForWindow(newAppViewWindow);
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newAppViewId,
ViewSizePreference.UseLess);
// The SDK documentation for Windows.UI.ViewManagement.ApplicationViewSwitcher explains
// its other methods, allowing you to control switching between your app's views.
}
前面的代码使用了Windows.UI.ViewManagement.ApplicationView类,这个类中的属性经常变,类如下:
public sealed class ApplicationView {
// Gets the view for the calling thread
public static ApplicationView GetForCurrentView();
// Gets the unique window ID corresponding to a specific CoreWindow
public static Int32 GetApplicationViewIdForWindow(ICoreWindow window);
// Gets a unique ID identifying this view. NOTE: This ID is passed to
// an XxxActivatedEventArgs' CurrentlyShownApplicationViewId property
public Int32 Id { get; }
// Gets/sets the view's title (shown in task switchers) & if PrtScn can capture its content
public String Title { get; set; }
public Boolean IsScreenCaptureEnabled { get; set; }
// Read-only properties related to view's position & size
public ApplicationViewOrientation Orientation { get; } // Landscape or Portrait
public Boolean AdjacentToLeftDisplayEdge { get; }
public Boolean AdjacentToRightDisplayEdge { get; }
public Boolean IsFullScreen { get; }
public Boolean IsOnLockScreen { get; }
// Raised when the view is removed from task switcher (if user closes the view)
public event TypedEventHandler<ApplicationView, ApplicationViewConsolidatedEventArgs>
Consolidated;
// Indicates if app terminates when all views close (Default=false)
public static Boolean TerminateAppOnFinalViewClose { get; set; }
}