question

javierv-3305 avatar image
0 Votes"
javierv-3305 asked javierv-3305 commented

How to write the Windows Initialization in pure C#?

The bootstrap code for the Windows platform uses App.xaml.cs and App.xaml

App.xaml

<maui:MauiWinUIApplication
    x:Class="ExampleMauiApp.WinUI.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:maui="using:Microsoft.Maui"
    xmlns:local="using:ExampleMauiApp.WinUI">
</maui:MauiWinUIApplication>

and

App.xaml.cs

using Microsoft.UI.Xaml;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace ExampleMauiApp.WinUI;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();  // (1)
    }

    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}


I want to replace the partial class and initialize the Application object programmatically without using the call to InitializeComponent that loads the XAML file.
Do you know what's required to do that? Maybe there is a builder or factory class that returns an application object so then I can programmatically setup additional
things.
I've tried to debug the code but for some reason, I can't load the debug symbols of Microsoft.UI.Xaml.Application.LoadComponent.
Just for info I've loaded all the symbols using Tools -> Options -> Debugging -> Symbols, but maybe I'm missing something.


Thanks.
--jv

dotnet-maui
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why do you want to write the Windows Initialization in pure C#?
Can you share some details about what is your expected case?

As noted in the Q&A FAQ: How to write a quality question

0 Votes 0 ·
javierv-3305 avatar image javierv-3305 LeonLu-MSFT ·

Why do you want to write the Windows Initialization in pure C#?

The goal to learn how to write the bootstrap code for Windows in pure C# without using XAML is because I want to use a different programming language. It will be easier for a proof of concept to not depend on XAML initially.

0 Votes 0 ·

If you do not add any code in App.xaml, please delete this.InitializeComponent(); line in the App constructor.

0 Votes 0 ·
javierv-3305 avatar image javierv-3305 LeonLu-MSFT ·

I've tried different things but I got a Runtime error since it seems I need to set up some Theme, that loaded via xaml file.

My current App class looks like this

using Microsoft.Maui;

namespace ExampleMauiApp.WinUI;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        
        }

    static void Main(string[] args)
    {
       var app = new App();
    }

    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}


The project builds but it doesn't work.




214472-image.png


0 Votes 0 ·
image.png (34.1 KiB)
LeonLu-MSFT avatar image LeonLu-MSFT javierv-3305 ·

Firstly, MAUI is open-source platform, you can find the source code in the MAUI GitHub page, then if you want to know bootstrap code for Windows in MAUI,

You can check MauiWinUIApplication code from this page: https://github.com/dotnet/maui/blob/3717ce0d385d1aadbd3b9795c203d438426aae17/src/Core/src/Platform/Windows/MauiWinUIApplication.cs

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();is MAUI application’s entrance when you load your application. No need to add static void Main(string[] args) method.

0 Votes 0 ·
Show more comments

0 Answers