Skip to content

Commit 956aebf

Browse files
Replace MainWindow with plain Window in sample projects (#2063)
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> This PR updates `WindowHelper.CreateWindow()` to return a plain `Window` instead of `MainWindow`, which was tightly coupled to several static members. ## Motivation and Context <!--- Why is this change required? What problem does it solve? --> <!--- If it fixes an open issue, please link to the issue here. --> Fixes #2062. ## How Has This Been Tested? <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> Manually tested. ## Screenshots (if appropriate): <img width="1115" height="793" alt="image" src="https://github.com/user-attachments/assets/ecd95d4d-a550-4d58-9a85-ec5243f304f2" /> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
1 parent f3112a4 commit 956aebf

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

WinUIGallery/Helpers/WindowHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class WindowHelper
1919
{
2020
static public Window CreateWindow()
2121
{
22-
MainWindow newWindow = new MainWindow();
22+
var newWindow = new Window();
2323
TrackWindow(newWindow);
2424
return newWindow;
2525
}

WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<StackPanel>
2222
<controls:ControlExample CSharpSource="Window/CreateWindowSample1.txt" HeaderText="Create single threaded Multiple Top level Windows(MTW).">
23-
<Button x:Name="Control1" Click="createNewWindow_Click">Create new Window</Button>
23+
<Button x:Name="Control1" Click="CreateNewWindow_Click">Create new Window</Button>
2424
</controls:ControlExample>
2525
</StackPanel>
2626
</Page>

WinUIGallery/Samples/ControlPages/CreateMultipleWindowsPage.xaml.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using Microsoft.UI.Xaml;
55
using Microsoft.UI.Xaml.Controls;
6+
using Microsoft.UI.Xaml.Media;
7+
using Windows.Graphics;
68
using WinUIGallery.Helpers;
7-
using WinUIGallery.Pages;
89

910
namespace WinUIGallery.ControlPages;
1011

@@ -15,14 +16,30 @@ public CreateMultipleWindowsPage()
1516
this.InitializeComponent();
1617
}
1718

18-
private void createNewWindow_Click(object sender, RoutedEventArgs e)
19+
private void CreateNewWindow_Click(object sender, RoutedEventArgs e)
1920
{
20-
var newWindow = new MainWindow();
21-
WindowHelper.TrackWindow(newWindow);
22-
newWindow.Activate();
21+
var childWindow = new Window()
22+
{
23+
ExtendsContentIntoTitleBar = true,
24+
SystemBackdrop = new MicaBackdrop(),
25+
Content = new Page()
26+
{
27+
Content = new TextBlock()
28+
{
29+
Text = "New child window!",
30+
HorizontalAlignment = HorizontalAlignment.Center,
31+
VerticalAlignment = VerticalAlignment.Center,
32+
},
33+
// Get the theme from the parent.
34+
RequestedTheme = this.ActualTheme,
35+
}
36+
};
2337

24-
var targetPageType = typeof(HomePage);
25-
string targetPageArguments = string.Empty;
26-
newWindow.Navigate(targetPageType, targetPageArguments);
38+
// We need to track the new window so it can be closed when the app is closing,
39+
// otherwise it will crash the app.
40+
// This is also used to change the theme for all windows when the app theme changes.
41+
WindowHelper.TrackWindow(childWindow);
42+
childWindow.AppWindow.ResizeClient(new SizeInt32(500, 500));
43+
childWindow.Activate();
2744
}
2845
}
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
// C# code to create a new window
2-
var newWindow = WindowHelper.CreateWindow();
3-
var rootPage = new NavigationRootPage();
4-
rootPage.RequestedTheme = ThemeHelper.RootTheme;
5-
newWindow.Content = rootPage;
6-
newWindow.Activate();
1+
// Ensure you close the child window before closing the parent window to avoid application crash.
2+
var childWindow = new Window()
3+
{
4+
ExtendsContentIntoTitleBar = true,
5+
SystemBackdrop = new MicaBackdrop(),
6+
Content = new Page()
7+
{
8+
Content = new TextBlock()
9+
{
10+
Text = "New child window!",
11+
HorizontalAlignment = HorizontalAlignment.Center,
12+
VerticalAlignment = VerticalAlignment.Center,
13+
},
14+
// Get the theme from the parent.
15+
RequestedTheme = this.ActualTheme,
16+
}
17+
};
718

8-
// C# code to navigate in the new window
9-
var targetPageType = typeof(HomePage);
10-
string targetPageArguments = string.Empty;
11-
rootPage.Navigate(targetPageType, targetPageArguments);
19+
childWindow.AppWindow.ResizeClient(new SizeInt32(500, 500));
20+
childWindow.Activate();

0 commit comments

Comments
 (0)