A Microsoft platform for building and publishing apps for Windows devices.
Hello,
Welcome to Microsoft Q&A.
I cannot reproduce the situation that there is a crash with a LayoutCycleException when a user presses the X in the upper right corner of the new window. But I reproduce the second situation that the PrintPage is displayed in the MainPage window after clicking the X in the new PrintPage window when debugging with F5.
You need to call the CoreWindow.Close method to exit the message loop in the Consolidated event handler.
Please check the following code:
CoreWindow coreWindow;
private async void Button_Click(object sender, RoutedEventArgs e)
{
printWindowId = -1;
var CoreViewPrint = CoreApplication.CreateNewView();
await CoreViewPrint.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => {
coreWindow = CoreWindow.GetForCurrentThread();
var applicationView = ApplicationView.GetForCurrentView();
Frame frame = new Frame();
applicationView.Consolidated += ApplicationView_Consolidated;
printWindowId = ApplicationView.GetApplicationViewIdForWindow(coreWindow);
frame.Navigate(typeof(PrintPage));
Window.Current.Content = frame;
printPageFrame = frame;
Window.Current.Activate();
applicationView.Title = "I am a title";
var inner = Window.Current;
});
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(printWindowId, ViewSizePreference.UseMore,
ApplicationView.GetForCurrentView().Id, ViewSizePreference.Default);
}
private void ApplicationView_Consolidated(ApplicationView sender, ApplicationViewConsolidatedEventArgs args)
{
if (coreWindow != null)
{
coreWindow.Close();
}
}
By adding the above code, you could test the SizeChanged event handler of PrintPage will not be trigger when you resize the size of MainPage window after closing the new PrintPage window. And, the content of MainPage window will not be replaced with the PringPage after closing the new created PrintPage window when debugging with F5.
Please tell me whether the situation that there is a crash with a LayoutCycleException when a user presses the X in the upper right corner of the new window will happen in your side when you using the above code.
Update:
Based on the document, we know that when we have the main view and the secondary view and clicking the close(X) button in the window title bar of the main view(MainPage), the main view’s window is hidden and its thread remains active. So the SizeChanged event handler of MainPage and the SizeChanged event handler of PrintPage will all be called when you close the MainPage window and resize the PrintPage window. Calling Close method on the main view’s window causes an InvalidOperationException to occur. If the main view’s thread is terminated, the app closes. Therefore, the better way is to use Application.Exit to close the whose app without leaving the PrintPage still shown.
If you want to close the secondary view(PrintPage), you need to call the Close() to release the window resource in the Consolidated event handler or other location before the window is closed by clicking the X icon.
I didn’t reproduce the situation that the SizeChanged event is fired before the Consolidated event. My SizeChanged event handlers are added by using the code Window.Current.SizeChanged += Current_SizeChanged; in the constructor of MainPage and PrintPage.
You could try to use another event SystemNavigationManagerPreview.CloseRequested event to replace the Consolidated event to see if it can resolve your problem. Note the necessary capability.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.