How can a universal windows app have multiple inde

2019-03-13 05:14发布

问题:

I do know how to open additional windows using TryShowAsStandaloneAsync. However, if the original window is closed - TryShowAsStandaloneAsync fails (Why?). And I don't know how to "revive" it (-the original window).

But "Photos" seems to work fine just like a desktop application. How does it do that? (I'd like to emulate that.) One can open a window with an image, open another one, close the first, and still be able to open more windows.

Any way would be fine - some way to launch windows without the main window showing, or some way of reviving the main window after it's closed (in order for it to be the new window that has to be opened), or some other way.

回答1:

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.



回答2:

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.



回答3:

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/