maui
maui copied to clipboard
Run without window on Windows
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.
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.
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 : 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.
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.
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.
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)
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
@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 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.
Any update on this feature? Is it something that is planned for MAUI at all?
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:
- Maui requires a MainPage to show on startup.
- 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();
}));
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:
- Maui requires a MainPage to show on startup.
- 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
Any update?