打开辅助窗口后,Windows.Current.Content的值应该是多少?

时间:2017-12-07 15:57:51

标签: c# uwp template10

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

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。

这是从the Template10 Secondary window example code:

打开辅助窗口(不带shell)的建议代码
 var control = await NavigationService.OpenAsync(typeof(MySecondaryPage), null, Guid.NewGuid().ToString());  

                control.Released += Control_Released;

Windows.Current.Content与辅助窗口之间有什么关系?

1 个答案:

答案 0 :(得分:2)

  

Windows.Current.Content与辅助窗口之间有什么关系?

实际上,NavigationService.OpenAsync方法在内部调用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;
}

从上面的代码中,你可以得到应用程序的Window(Singleton)没有被更改,当第二个窗口被激活时,Content的{​​{1}}被替换为第二页。当前一个窗口激活时,Window将返回第一页。您可以使用Windows.Current.Content事件验证这一点。

Window.Current.Activated