UWP problems with multiple views

你。 提交于 2019-11-30 20:13:07

Actually the proper way to still be able to open up new windows after the main one is closed is to use one of the overloads provided by TryShowAsStandaloneAsync.

protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    // Create the newWindowId and stuff...

    await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newWindowId, 
        ViewSizePreference.Default,
        e.CurrentlyShownApplicationViewId, 
        ViewSizePreference.Default);

Basically, you need to specify the third parameter anchorViewId which is

the ID of the calling (anchor) window.

In this case, you just need to pass in e.CurrentlyShownApplicationViewId.

I've found the solution to my problems, and I've actually decided not to use the ViewLifeTime control that comes with the sample.

The problem is that when the Main view is closed you have to use the Dispatcher.RunAsync() method from one of the other views that are still open to run it that thread

Here's the code that I've changed in my App.xaml.cs for anyone that is interested:

public bool isMainViewClosed = false;
public ObservableCollection<CoreApplicationView> secondaryViews = new ObservableCollection<CoreApplicationView>();

//...

protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame == null)
        {
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            alreadyLaunched = true;
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        else if(alreadyLaunched)
        {
    //If the main view is closed, use the thread of one of the views that are still open
            if(isMainViewClosed)
            {
                int newViewId = 0;
                await secondaryViews[0].Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var currentPage = (MainPage)((Frame)Window.Current.Content).Content;
                    Window.Current.Activate();
                    currentPage.NewWindow();
                    newViewId = ApplicationView.GetForCurrentView().Id;
                });
                bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
            }
            else
            {
                CoreApplicationView newView = CoreApplication.CreateNewView();
                int newViewId = 0;
                await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Frame frame = new Frame();
                    frame.Navigate(typeof(MainPage), null);
                    Window.Current.Content = frame;
                    var currentPage = (MainPage)((Frame)Window.Current.Content).Content;
                    Window.Current.Activate();

                    secondaryViews.Add(CoreApplication.GetCurrentView());
                    newViewId = ApplicationView.GetForCurrentView().Id;
                });
                bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
            }
        }
        Window.Current.Activate();
    }
sunnyonthecoast

don't View(your)Lifetime away ... cheers,

int idCreate = 0; List<int> idSaved = new List<int>();
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        rootFrame.NavigationFailed += OnNavigationFailed;
        Window.Current.Content = rootFrame;
    }

    if (rootFrame.Content == null)
    {
        rootFrame.Navigate(typeof(MainPage), e.Arguments);
        idSaved.Add(ApplicationView.GetForCurrentView().Id);
    }
    else
    {
        var create = CoreApplication.CreateNewView(); 
        await create.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            var frame = new Frame();
            frame.Navigate(typeof(MainPage), e.Arguments);
            Window.Current.Content = frame;
            Window.Current.Activate();

            idCreate = ApplicationView.GetForCurrentView().Id;
        });

        for(int i = idSaved.Count - 1; i >= 0; i--)
            if (await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
                    idCreate, ViewSizePreference.UseMinimum, 
                    idSaved[i], ViewSizePreference.UseMinimum)
               ) break;

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