app .xaml cs in net 9

Dani_S 4,521 Reputation points
2025-01-29T10:21:08.6866667+00:00

Hi,

How to convert this code in maui net 9.

using Autofac;

using GssdDesktopClient.API.Imp;

using GssdDesktopClient.Maui.Helpers;

using GssdDesktopClient.Maui.Pages;

using GssdDesktopClient.Maui.Startup;

using Microsoft.Maui.Controls.PlatformConfiguration;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Reflection;

using System.Runtime.ExceptionServices;

using System.Text;

namespace GssdDesktopClient.Maui

{
public partial class App : Application

 {

private static Mutex mutex = new Mutex(true, Assembly.GetEntryAssembly().GetName().Name);

public App()

 {

   if (!mutex.WaitOne(TimeSpan.Zero, true))

{

Current.Quit();

Environment.Exit(0);

 }

InitializeComponent();

AppDomain currentDomain = AppDomain.CurrentDomain;

currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

MainPage = new LoginPage();

 }

private async void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

 {

try

 {

if (!string.IsNullOrEmpty(LoginConfigurations.GetInstance().SessionToken))

 {

var container = new Bootstrapper().Bootstrap();

IAutomationApiClient automationApiClient = container.Resolve<IAutomationApiClient>();

var res = await automationApiClient.PostLogOut($"{LoginConfigurations.GetInstance().Portal}/api/GSPortal/PostLogOut/false", LoginConfigurations.GetInstance().SessionToken);

 }

 }

catch (Exception ex)

{

 }

  }

protected override Window CreateWindow(IActivationState activationState)

 {

Window window = base.CreateWindow(activationState);

try

 {

window.Stopped += async(s,e) =>

 {

if (!string.IsNullOrEmpty(LoginConfigurations.GetInstance().SessionToken ))
 {

var container = new Bootstrapper().Bootstrap();

IAutomationApiClient automationApiClient = container.Resolve<IAutomationApiClient>();

var results = await automationApiClient.PostLogOut($"{LoginConfigurations.GetInstance().Portal}/api/GSPortal/PostLogOut/false", LoginConfigurations.GetInstance().SessionToken);

if(results)

 {
App.Current.Quit();

 }

 }

 };

 }
catch (Exception)

 {

 }

return window;

 }

 }

}

when running app in debug it works but without debugging is not.

error:

XAML: .NET Runtime version currently executing is 8.0.5, but version 8.0.10 or later is required.

% dotnet --list-runtimes

Microsoft.AspNetCore.App 8.0.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]

Microsoft.AspNetCore.App 9.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]

Microsoft.NETCore.App 8.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Microsoft.NETCore.App 9.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

This is my dotnet workload list

Last login: Thu Jan 30 08:23:38 on ttys005

sasasoftware@Sasas-MacBook-Air ~ % dotnet workload list

Installed Workload Id Manifest Version Installation Source

maui 9.0.14/9.0.100 SDK 9.0.100

Use dotnet workload search to find additional workloads to install.

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-01-29T17:19:30.8533333+00:00

    this code looks like it uses a global mutex to allow only one instance of the app. uses an injected object to log application main window open close, and uncaught errors.

    you will need to design a solution for each O/S. while the global mutex will work on linux and MacOs, it will be buggy (false detection of running app) due to the mutex implementation code. not needed for mobile O/S.

    the Maui lifecycle events match the mobile o/s where apps are visible or not visible, background, active, or stopped.


  2. Harry Vo (WICLOUD CORPORATION) 405 Reputation points Microsoft External Staff
    2025-08-13T06:45:50.1533333+00:00

    Hi @Dani_S ,

    My name is Harry, Support Engineer who specialize in UWP and MAUI. Thank you for reaching out on Microsoft Q&A!

    From what you’ve described, your application is failing to launch because it’s running on the .NET 8.0.5 runtime, while one of your dependencies (likely Microsoft.Maui.Controls.Xaml) requires at least .NET 8.0.10. You can quickly fix this issue by update your .NET 8 runtime to version 8.0.10 or higher from our official download link, then verifying with dotnet --list-runtimes.

    Moreover, fully migrate your project to .NET 9 can also solve this problem. You can follow these step:

    1. Changing the <TargetFramework> in your .csproj to net9.0
    2. Updating all NuGet dependencies to their .NET 9-compatible versions

    As an optional note, your mutex approach is fine for Windows, but it can behave inconsistently in other platform like macOS or Linux. I highly recommend you add platform-specific logic for this.

    I hope this helps you get things back on track quickly! If my suggestions can solve your issue, feel free to interact with the system accordingly!

    Thank you!

    0 comments No comments

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.