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

限于喜欢 提交于 2019-12-11 02:35:45

问题


This is the suggested code in the App class from the Template10 article on implementing a shell:

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

The new shell object is assigned to Windows.Current.Content.

This is the suggested code for opening a secondary window (not with a shell), from the Template10 Secondary window example code:

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

                control.Released += Control_Released;

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


回答1:


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

Actually, The 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;
}

From the above code, you could get the app's Window(Singleton) has not been changed, when the second window activated, the Content of Window was replaced with second page. And when the previous window activated the Windows.Current.Content will turn the fist page back. And you could verify this with Window.Current.Activated event.

Window.Current.Activated += Current_Activated;

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


来源:https://stackoverflow.com/questions/47698947/what-should-the-value-of-windows-current-content-be-after-opening-a-secondary-wi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!