C
C#6mo ago
DaClownie

New ContentPages in .net MAUI app don't have InitializeComponent() methods

I'm following a few guides to learn how to do Navigation in a .NET MAUI app which I need to apply for a project for school. All of the guides show creation of multiple ContentPages, assigning it as such in App.xaml.cs
MainPage = new NavigationPage(new TermPage());
MainPage = new NavigationPage(new TermPage());
I created a new ContentPage in the solution named TermPage but it doesn't have a InitializeComponent() and I'm unable to Build my project and send it to the Android Emulator. How come the guides have no issue with the creation of the InitializeComponent() method and mine does? And How do I fix it?
9 Replies
Absent_Reality
Absent_Reality6mo ago
I've not tried to add a page that way, but the easiest I've found so far is to add it in the xaml in app.shell like
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MyApp.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp"
Shell.FlyoutBehavior="Disabled"
Title="MyApp">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MyApp.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp"
Shell.FlyoutBehavior="Disabled"
Title="MyApp">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
Then if you are adding a new page then you would add the
<ShellContent Title="TermPage" ContentTemplate="{DataTemplate local:TermPage}" Route="TermPage" /> The top one you add, like mainPage for instance, will be the first one it goes to. When I add a page in vs, I add it as .Net Maui ContentPage(xaml). The InitializeComponent will already be there for you.
DaClownie
DaClownieOP6mo ago
That’s where I’m struggling. When I add a page in VS, I’m also adding it as a ContentPage(XAML) and it is not creating the InitializeComponent method. It puts the call to it in the body of the class but it isn’t linked to anything. I figured I was doing something wrong.
Absent_Reality
Absent_Reality6mo ago
Interesting. I just tried on a new project. Right click on the project name, just under the solution, then add, then new item. If that isn't adding it, idk. Hmm... Do you have your namespace at the top? I know I've forgot it before and the Initialize will give an error.
DaClownie
DaClownieOP6mo ago
Fairly certain I do, but I’ll remote in to my desktop in a bit and check. I added all my usings above the namespace right after creation if I remember correctly.
Absent_Reality
Absent_Reality6mo ago
May be out of my league if that isn't it. Lol
DaClownie
DaClownieOP6mo ago
Just to test, I created A TestPage ContentPage XAML namespace exists, its brand new, created by VS in the project, changed nothing. It auto adds the call for InitializeComponent(); but its not backed and the project won't build. I have to comment them out. I'm at a complete loss for what to do next @Absent_Reality closing and relaunching Visual studio seems to have fixed the issue. I battled this for hours this afternoon between google searches, looking at settings, deleting and recreating pages, etc. What a weird bug.
Absent_Reality
Absent_Reality6mo ago
Awesome! Glad you got it! I should have thought of that as well. I know sometimes Maui has issues when adding and deleting things like images where it hiccups and tells you it can't find something. Seems to help if I hit clean solution under the build tab.
DaClownie
DaClownieOP6mo ago
I’ll give that a try next time and see that if that resolves it rather than IDE restart. Thanks again!
Absent_Reality
Absent_Reality6mo ago
No problem!
Want results from more Discord servers?
Add your server