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
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
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
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.
<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.
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.
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.
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.
May be out of my league if that isn't it. Lol
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.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.
I’ll give that a try next time and see that if that resolves it rather than IDE restart. Thanks again!
No problem!