app .xaml cs in net 9

Dani_S 4,036 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;

 }

 }

}

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

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 70,376 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.