3

I am able to start my Windows MAUI app using an URI, and I can get the URI itself. But, it appears that a NEW instance of the app is being created. This is not ideal for me -- if my app is already running, I want to use that instance.

I have done something like this for a Xamarin.Forms app. I override OnActivated in Application class.

Re: my MAUI app, I'm not even clear on whether the issue is how I've done the "protocol" in package.appxmanifest, or if it is how I respond to lifecycle events.

bobwki
  • 794
  • 6
  • 22

2 Answers2

2

This solution basically, if you have any open instances, you cannot open new instance. Because WaitOne function is detect another instance and close new program before create new ones.(App.xaml.cs) (.NET 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();
       MainPage = new AppShell();

   }
}
1

The default behaviour is to run multiple instances of your app. You can make the app single-instanced by defining a customized class with a Main method as suggested in this blog post:

[STAThread]
static async Task Main(string[] args)
{
    WinRT.ComWrappersSupport.InitializeComWrappers();
    bool isRedirect = await DecideRedirection();
    if (!isRedirect)
    {
        Microsoft.UI.Xaml.Application.Start((p) =>
        {
            var context = new DispatcherQueueSynchronizationContext(
                DispatcherQueue.GetForCurrentThread());
            SynchronizationContext.SetSynchronizationContext(context);
            new App();
        });
    }
    return 0;
}

private static async Task DecideRedirection()
{
    bool isRedirect = false;
    AppActivationArguments args = AppInstance.GetCurrent().GetActivatedEventArgs();
    ExtendedActivationKind kind = args.Kind;
    AppInstance keyInstance = AppInstance.FindOrRegisterForKey("randomKey");

    if (keyInstance.IsCurrent)
    {
        keyInstance.Activated += OnActivated;
    }
    else
    {
        isRedirect = true;
        await keyInstance.RedirectActivationToAsync(args);
    }
    return isRedirect;
}

There is an open suggestion to simplify this process available on GitHub.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    Thanks for the pointer. I ended up going to the article, but that did not get me far enough, so I went to the GitHub repo referenced. And, I added One point of confusion for MAUI, the "new App()" must reference the WINDOWS App class, not the generic. So I put the namespace in Program.cs code as .WinUI – bobwki Oct 25 '22 at 17:46
  • Also, the mod to the add DISABLE_XAML_GENERATED_MAIN did not work for me -- I put this instead using the "property" editor for the project. Oh, and one last thing, this appears to require windows10.0.19041 or better (17736 did not work). – bobwki Oct 25 '22 at 17:49
  • @bobwki I have the problem where I can't reference the windows app class, but only the one in root directory – YaRmgl May 18 '23 at 06:47