app .xaml cs in net 9

Dani_S 4,081 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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,915 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,266 Reputation points
    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.


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.