maui icon indicating copy to clipboard operation
maui copied to clipboard

Run without window on Windows

Open Tealons opened this issue 3 years ago • 12 comments

Description

We have WPF application that we would like to port to MAUI. This application sometimes runs on the background via a command line argument. In this case we just don't show any window with the WPF application. We tried to also enable this with the MAUI Windows application, but it seems that it always expects to open a window?

Is it possible to add an enhancement that enables us to run a .NET MAUI windows application without opening a window?

Public API Changes

In WPF we removed the StartupUri and implemented a custom OnStartup method to have the option to decide if we wanted to open a window in our startup or not. I'm not sure where this would fit in .NET MAUI?

Intended Use-Case

Running the application without any interface. This could sound strange because it somewhat defies the purpose of .NET MAUI, but is currently helps us in deploying some extra background tasks easy (via the task scheduler) at our customers sites without the need to create a service.

Tealons avatar Dec 23 '21 13:12 Tealons

Upvote! My use case - app runs silently in background and create tray icon. But no need to show UI at moment of running, actually can but we have an option allow user to choose if need to show UI on start or run silently until he will click tray icon.

arkadym avatar Dec 28 '21 07:12 arkadym

I don't know your use-case well, but this is what I am doing: I have a core library with all the models/viewmodels data persistence and business logic. I have a console app that consumes my library, and a Maui App that consumes the same library. They are all 3 in the same Solution. The only code not in the core library is what is unique to a Maui app or a Console app. This allows me to focus on the behavior necessary for each one cleanly without trying to use either system in a way it wasn't meant.

You could make the shortcut default to the console app but have a flag that allows the console to launch the maui app for instance.

chris-ux avatar Dec 28 '21 20:12 chris-ux

@chris-ux : We also considered that approach. But that will require us to install and update two applications in our customer environments. It's just simpler when the same application has two modes. With and without a window.

Tealons avatar Dec 29 '21 15:12 Tealons

Agreed here. I want to use MAUI for one of my tools, but I want it to start as a tray application only, until tray menu choice is actually made. There's no reason why this shouldn't be supported.

TehGM avatar May 01 '22 15:05 TehGM

We've moved this issue to the Future milestone. This means that it is not going to be worked on for the coming release. We will reassess the issue following the current release and consider this item at that time.

ghost avatar Jun 15 '22 10:06 ghost

I'm a product manager /owner at a B2B company and this issue is a huge blocker for us for utilizing .NET MAUI for Windows and MacOS, because our apps should be able to run in background without any windows (only tray)

DavidOsipov avatar Jul 28 '22 13:07 DavidOsipov

This would also be really useful to have on Android, IOS, etc. since we can make it so you get a notification when there is a special event in your application or like in games where you have a storage and it will give you a notification when your storage is full

Mielesgames avatar Oct 19 '22 13:10 Mielesgames

@arkadym Hi! You mentioned a use case where you would have just the tray icon and no actual app window. this is exactly what i am trying to build, but i have much troubles. would you be able to show me how to remove the app window and how to add a tray icon? and how could i make a right-click menu on the tray icon? thanks a lot!! btw, i need this for a mac application in specific.

ainotna avatar Mar 30 '23 10:03 ainotna

@ainotna didnt yet implemented it. Should be possible via creating platform specific service and put all native windows tray icon handling. Hiding of main window have to be done there too.

arkadym avatar Mar 30 '23 11:03 arkadym

Any update on this feature? Is it something that is planned for MAUI at all?

skostore avatar Oct 13 '23 17:10 skostore

Welp, just spent the day trying to do what others did. Maui app that runs in the tray until user opens it. Ran into two issues:

  1. Maui requires a MainPage to show on startup.
  2. Maui doesn't support Context menus in the sys tray. I used the sample code to get the sys tray icon, which apparently was taken from hardcodet.net NotifyIcon for WPF without a context menu solution :(

I've tried various things to try and hide the window on create and it still shows up. Example:

           lifecycle
               .AddWindows(windows =>
                   windows.OnPlatformMessage((app, args) =>
                   {
                       if (WindowExtensions.Hwnd == IntPtr.Zero)
                       {
                           WindowExtensions.Hwnd = args.Hwnd;
                           WindowExtensions.SetIcon("Platforms/Windows/trayicon.ico");
                       }
                   }));
           lifecycle.AddWindows(windows => windows.OnWindowCreated((win) =>
           {
               win.AppWindow.Hide();
               WindowExtensions.MinimizeToTray();
           }));

mikebm avatar Nov 16 '23 22:11 mikebm

Welp, just spent the day trying to do what others did. Maui app that runs in the tray until user opens it. Ran into two issues:

  1. Maui requires a MainPage to show on startup.
  2. Maui doesn't support Context menus in the sys tray. I used the sample code to get the sys tray icon, which apparently was taken from hardcodet.net NotifyIcon for WPF without a context menu solution :(

I've tried various things to try and hide the window on create and it still shows up. Example:

           lifecycle
               .AddWindows(windows =>
                   windows.OnPlatformMessage((app, args) =>
                   {
                       if (WindowExtensions.Hwnd == IntPtr.Zero)
                       {
                           WindowExtensions.Hwnd = args.Hwnd;
                           WindowExtensions.SetIcon("Platforms/Windows/trayicon.ico");
                       }
                   }));
           lifecycle.AddWindows(windows => windows.OnWindowCreated((win) =>
           {
               win.AppWindow.Hide();
               WindowExtensions.MinimizeToTray();
           }));

Hi @mikebm, I resolved with this code:

#if WINDOWS
               lifecycle.AddWindows(lifecycleBuilder => 
                   lifecycleBuilder.OnWindowCreated(window =>
                   {
                       System.Diagnostics.Debug.WriteLine("OnWindowCreated");
                       window.ExtendsContentIntoTitleBar = true;
                       var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                       var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                       var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);

                       WindowExtensions.Hwnd = handle;
                       WindowExtensions.MinimizeToTray(); //this doesn't works                                                                
                   }
               ));

               lifecycle.AddWindows(lifecycleBuilder =>
                  lifecycleBuilder.OnLaunched((del, s) =>
                  {
                      System.Diagnostics.Debug.WriteLine("OnLaunched");
                      WindowExtensions.MinimizeToTray(); //this works
                  }));
#endif

BladeBaT avatar May 13 '24 08:05 BladeBaT

Any update?

Metalvast avatar Jul 25 '24 19:07 Metalvast