Starting app

Eduardo Gomez Romero 1,355 Reputation points
2024-11-13T05:20:41.87+00:00

I guess that because .Net 9 define the main Page as deprecated, we get errors, now I cannot run my app

errors

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS1061	'App' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)	mauiNotes (net8.0-windows10.0.19041.0)	C:\Users\egome\Documents\Maui Course\mauiNotes\Platforms\Windows\App.xaml.cs	16		


Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS0246	The type or namespace name 'MauiWinUIApplication' could not be found (are you missing a using directive or an assembly reference?)	mauiNotes (net8.0-windows10.0.19041.0)	C:\Users\egome\Documents\Maui Course\mauiNotes\Platforms\Windows\App.xaml.cs	10		

namespace mauiNotes {
    public partial class App : Application {

        public App(AppShell appShell) {
            InitializeComponent();

            MainPage = appShell;
        }
    }
}
Severity	Code	Description	Project	File	Line	Suppression State	Details
Warning (active)	CS0618	'Application.MainPage.set' is obsolete: 'This property is deprecated. Initialize your application by overriding Application.CreateWindow rather than setting MainPage. To modify the root page in an active application, use Windows[0].Page for applications with a single window. For applications with multiple windows, use Application.Windows to identify and update the root page on the correct window.  Additionally, each element features a Window property, accessible when it's part of the current window.'	mauiNotes (net8.0-android), mauiNotes (net8.0-ios), mauiNotes (net8.0-maccatalyst), mauiNotes (net8.0-windows10.0.19041.0)	C:\Users\egome\Documents\Maui Course\mauiNotes\App.xaml.cs	7		


Question

why the do this

Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-11-13T07:26:32.8466667+00:00

    Hello,

    In the .net 9.0 release Maui updated the way to set up MainPage.

    Please refer to What's new in .NET MAUI for .NET 9 for more details.

    Instead of defining the first page of your app using the MainPage property on an Application object, you should set the Page property on a Window to the first page of your app. This is what happens internally in .NET MAUI when you set the MainPage property, so there's no behavior change introduced by the MainPage property being marked as obsolete. The following example shows setting the Page property on a Window, via the CreateWindow override:

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    
        protected override Window CreateWindow(IActivationState? activationState)
        {
            return new Window(new AppShell());
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.