开启副window后Windows.Current.Content的值应该是多少?

What should the value of Windows.Current.Content be after opening a secondary window?

这是 implementing a shell 上 Template10 文章中 App class 中的建议代码:

public override Task OnInitializeAsync(IActivatedEventArgs args)
{
    var nav = NavigationServiceFactory(BackButton.Attach, ExistingContent.Include);
    Window.Current.Content = new Views.Shell(nav);
    return Task.FromResult<object>(null);
}

新的 shell 对象分配给 Windows.Current.Content。

这是打开辅助 window(不是 shell)的建议代码,来自 the Template10 Secondary window example code:

 var control = await NavigationService.OpenAsync(typeof(MySecondaryPage), null, Guid.NewGuid().ToString());  

                control.Released += Control_Released;

Windows.Current.Content和二级window有什么关系?

What is the relationship between Windows.Current.Content and the secondary window?

实际上,NavigationService.OpenAsync method internally invoke ViewService.OpenAsync

public async Task<IViewLifetimeControl> OpenAsync(UIElement content, string title = null,
    ViewSizePreference size = ViewSizePreference.UseHalf)
{
    this.Log($"Frame: {content}, Title: {title}, Size: {size}");

    var currentView = ApplicationView.GetForCurrentView();
    title = title ?? currentView.Title;

    var newView = CoreApplication.CreateNewView();
    var dispatcher = DispatcherEx.Create(newView.Dispatcher);
    var newControl = await dispatcher.Dispatch(async () =>
    {
        var control = ViewLifetimeControl.GetForCurrentView();
        var newWindow = Window.Current;
        var newAppView = ApplicationView.GetForCurrentView();
        newAppView.Title = title;

        // TODO: (Jerry)
        // control.NavigationService = nav;

        newWindow.Content = content;
        newWindow.Activate();

        await ApplicationViewSwitcher
            .TryShowAsStandaloneAsync(newAppView.Id, ViewSizePreference.Default, currentView.Id, size);
        return control;
    }).ConfigureAwait(false);
    return newControl;
}

从上面的代码可以看出app的Window(Singleton)没有改变,当第二个window激活时,WindowContent被第二页取代。当上一个 window 激活时 Windows.Current.Content 将翻回第一页。你可以用 Window.Current.Activated 事件来验证这一点。

Window.Current.Activated += Current_Activated;

private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
 // check the sender
}