How can a universal windows app have multiple independent windows (Like Microsoft's app “Photos”)?

后端 未结 3 1003
执念已碎
执念已碎 2021-02-04 10:50

I do know how to open additional windows using TryShowAsStandaloneAsync. However, if the original window is closed - TryShowAsStandaloneAsync fails (Wh

相关标签:
3条回答
  • 2021-02-04 11:23

    Have a look at the MultipleViews sample app. This app does have the problem you're describing.

    Each view that you create will have their own UI Thread, and therefore dispatcher. The key to this app is that TryShowAsStandaloneAsync is called from the dispatcher of the currently active window.

    In the sample's OnLaunched event, the code looks for a reference to the currently open view, using the view id from the launch arguments. It then uses the dispatcher associated with that view to call UI code, using Dispatcher.RunAsync, on that view's UI thread. You should use that thread of the open window to call TryShowAsStandaloneAsync to launch your new main view. You can then call Window.Activate using the new main view's dispatcher.

    0 讨论(0)
  • 2021-02-04 11:28

    Answering why TryShowAsStandaloneAsync fails once you have closed the main window:

    I think TryShowAsStandaloneAsync tries to use the main view as the anchor view (ie, a window to place the new window relative to).

    Once you close the main window TryShowAsStandaloneAsync fails because it has no anchor view.

    The workaround is to specify an anchorViewId of a view that is open (one of the new windows you opened prior to closing the main window), via an overload of TryShowAsStandaloneAsync:

    await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
        viewIdToShow, // Id of a new view, or of your hidden main view
        ViewSizePreference.Default,
        anchorViewId, // Id of one of your visible windows
        ViewSizePreference.Default);
    

    From this answer.

    0 讨论(0)
  • 2021-02-04 11:31

    I'm not sure if you're using Dispatcher.RunAsync to create the view, i.e:

    async private void Button_Click(object sender, RoutedEventArgs e)
     {
     CoreApplicationView newView = CoreApplication.CreateNewView();
     int newViewId = 0;
    
    await newView.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
     {
     var frame = new Frame();
     frame.Navigate(typeof(MainPage), newViewId);
     Window.Current.Content = frame;
    
    // In Windows 10 UWP we need to activate our view first.
     // Let's do it now so that we can use TryShow...() and SwitchAsync().
     Window.Current.Activate();
    
    newViewId = ApplicationView.GetForCurrentView().Id;
     });
    
     bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
     }
    

    for more info, please refer to : https://daxsnippets.wordpress.com/2015/07/09/windows-10-create-views/

    0 讨论(0)
提交回复
热议问题