Search code examples
xamarin.formsuwpmauiwinui-3

Migrating UWP with Xamarin Forms Project to MAUI with WinUI 3 File Structure and Logic Placement


I'm in the process of migrating a UWP with Xamarin Forms project to a .NET MAUI with WinUI 3 project. I'm trying to understand where I should place my existing logic in the new project structure.

Existing Project Structure:

Xamarin Forms Project:

  • App.xaml
  • App.xaml.cs
  • ViewModels

UWP Project:

  • App.xaml
  • App.xaml.cs (registering all the dependencies)
  • MainPage.xaml
  • MainPage.xaml.cs
  • Dependency Services (in separate folders)

MAUI Project Structure: New MAUI Multi-Project App (selected only WinUI 3 for Windows):

MAUI Project:

  • App.xaml
  • App.xaml.cs
  • AppShell.xaml
  • AppShell.xaml.cs
  • MainPage.xaml
  • MainPage.xaml.cs
  • MauiProgramExtension.cs

MAUI WinUI3 Project:

  • App.xaml
  • App.xaml.cs
  • MauiProgram.cs

I'm not sure where to move the logic from the Xamarin Forms with UWP project to the MAUI with WinUI 3 project. Specifically, I'm struggling with understanding:

Where should I move the dependency registrations from App.xaml.cs? What is the equivalent of the AppShell, and where should the navigation logic be placed?

How should I structure the MainPage.xaml and MainPage.xaml.cs in the new project?

What should be included in MauiProgram.cs and MauiProgramExtension.cs?

I've not found much documentation or tutorials that address this specific migration scenario. Any guidance or examples would be greatly appreciated.


Solution

  • Where should I move the dependency registrations from App.xaml.cs? What should be included in MauiProgram.cs and MauiProgramExtension.cs?

    You can put dependency registrations into MauiProgramExtension.cs like this:

    ....
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
    
            builder.Services.AddTransient<ILoggingService, LoggingService>();
            builder.Services.AddTransient<ISettingsService, SettingsService>();
            builder.Services.AddSingleton<MainPageViewModel>();
            builder.Services.AddSingleton<MainPage>();
    .....
    

    MAUI provides in-built support for using dependency injection. For more info you can refer to the official doc: Dependency injection. In addition, you can also see this case: Difference between ".NET MAUI Multi-project App" and ".NET MAUI App". For .NET MAUI App project, there is only a MauiProgram.cs in the root directory. But for .NET MAUI Multi-project App project, It was split into two: MauiProgram.cs and MauiProgramExtension.cs.

    What is the equivalent of the AppShell, and where should the navigation logic be placed?

    You can choose not to use it, to delete it. It is the template generated by default. You can also use other styles

    How should I structure the MainPage.xaml and MainPage.xaml.cs in the new project?

    See this: Remove files. You can set layout and control in MAUI Project:

    • App.xaml
    • App.xaml.cs
    • AppShell.xaml
    • AppShell.xaml.cs
    • MainPage.xaml
    • MainPage.xaml.cs
    • MauiProgramExtension.cs