question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: WinUI 3 - File New Project Template should create a `MainPage` and not just a MainWindow

See original GitHub issue

_Originally posted by @michael-hawker in https://github.com/microsoft/microsoft-ui-xaml/issues/4966#issuecomment-882958799_

Yeah, I too keep getting tripped up that Window isn’t a DependencyObject… it’s a bit weird.

If the expectation that Window is supposed to just be the host like the implicit Frame created in App.xaml.cs for UWP apps, maybe the File -> New template for WinUI 3 should separate out the MainWindow as also having a MainPage class hosted by it already setup?

This would help lead developers to the proper constructs when building their app and set them up for success vs. having everyone start dumping UI in MainWindow and then getting frustrated.

I mean I really like how the MainWindow is more exposed compared to all the extra logic on Startup that was done in the UWP space, but it’s super confusing and bare-bones template when trying to start building an app from afterwards without creating a new Page first.

I keep seeing a few issues filed because of this (and have been tripped up myself) by the fact that the default project template for WinUI 3 creates a bare Window class.

This doesn’t jive as well with many UI APIs as it’s not a DependencyObject/FrameworkElement. It’s really just the application ‘shell’, like the implicit Frame that UWP apps used to create back in App.xaml.cs.

I love that this Window is now exposed and more easily customizable via XAML, but feel like it should still host a Frame which then points to a MainPage object based on Page where a developer is then led to start creating their application’s UI.

Otherwise, developers just start building UI within the Window itself and get tripped up when something needs to navigate the visual tree or try to create a DependencyProperty off of it (see #4966 for instance).

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
michael-hawkercommented, Feb 25, 2023

If anything, the template could just create a MainPage and reference it in the Window XAML:

<Window x:Class="ProjectTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:ProjectTemplate"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

    <!--  Window isn't a FrameworkElement for dependency property or resources...  -->
    <local:MainPage />
</Window>

This is what I did in my template (though I’m also sharing the MainPage with the UWP head in the case anyway): https://github.com/michael-hawker/WinUI2Plus3Template/blob/main/ProjectTemplate.WinAppSDK/MainWindow.xaml

Though in the onboarding/getting started docs there should still be information here about how to setup and use a Frame model (if needed) like what is setup by default in a UWP app or alternatively hosting a NavigationView with a Frame inside it. Though it definitely needs to be called out somewhere that Window isn’t mean to be used with XAML markup constructs, see folks running into this pitfall, mostly with binding, all the time.

1reaction
crramirezcommented, Jul 20, 2021

Ok, I will begin with that. I agree with you that this could help everyone to migrate a UWP app to WinUI 3 easier.

I copied the stats from another user 😏 . This is the code:

![Carlos Ramirez's GitHub Stats](https://github-readme-stats.vercel.app/api?username=crramirez&show_icons=true&count_private=true)

Read more comments on GitHub >

github_iconTop Results From Across the Web

WinUI 3 templates in Visual Studio - Windows apps
This topic describes the available project and item templates. And Create your first WinUI 3 project walks you through creating a project.
Read more >
Unwrapping WinUI3 for C++ - Marius Bancila's Blog
From the available list then select Blank App, Packaged (WinUI 3 in Desktop): The following template project is created: The solution contains ...
Read more >
Error opening XAML page in designer in a WinUI 3 ...
WinUI 3 doesn't have a XAML designer and doesn't work with Blend. There's a ongoing discussion in the repo here. We'll might get...
Read more >
First Steps with Telerik VS Extension - Telerik UI for WinUI
The Create New Project Wizard dialog provides the options to select your preferred target platform (UWP or Desktop) and project template (Empty or...
Read more >
About the main window cannot be closed in full screen mode
When in full screen mode, after pressing the close button for the first time, it will jump to the new project page, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found