Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WinUI3 windowless] Crashes when TaskbarIcon is the only element in ResourceDictionary #196

Open
koharubiyori opened this issue Jan 28, 2025 · 9 comments
Labels
bug Something isn't working

Comments

@koharubiyori
Copy link

Describe the bug

Based on WinUI - Windowless, the message when exiting: xxx.exe has exited with code 3221226525 (0xc000041d).

TrayIcon.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tb="using:H.NotifyIcon"
>
	<tb:TaskbarIcon
        x:Key="TrayIcon"
        x:Uid="TrayIcon"
        Visibility="Visible"
        MenuActivation="LeftOrRightClick"
        ContextMenuMode="SecondWindow"
        NoLeftClickDelay="True"
        >
		<tb:TaskbarIcon.ContextFlyout>
			<MenuFlyout>
				<ToggleMenuFlyoutItem x:Uid="StartupSwitch" x:Name="StartupSwitch" />
				<ToggleMenuFlyoutItem x:Uid="LanOnlySwitch" x:Name="LanOnlySwitch" />
				<MenuFlyoutItem x:Uid="ExitButton" x:Name="ExitButton" />
			</MenuFlyout>
		</tb:TaskbarIcon.ContextFlyout>
	</tb:TaskbarIcon>
</ResourceDictionary>

Steps to reproduce the bug

No response

Expected behavior

No response

Screenshots

No response

NuGet package version

2.2.0

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 11

WindowsAppSDK Version

Other

WindowsAppSDK Type

Unpackaged

Manifest

No response

Additional context

No response

@koharubiyori koharubiyori added the bug Something isn't working label Jan 28, 2025
@TWiesendanger
Copy link

I had some problems with this library combined with winui3. Can you try to add the Taskbar icon in code and make sure that you save it in a static variable? In my case the garbage collector removed the reference.

@TheRealHuzy
Copy link

I am currently running into some issues with this as well (winUI3). Can't seem to even make the sample project run. Would you TWiesendanger mind providing a simple setup guide on how you managed to use this plugin in your winUI app? Just the .net version, plugin version and minimal code needed to display a notify/tray icon. Whatever I try seems to lead to different unwanted outcome.

@koharubiyori
Copy link
Author

@TheRealHuzy A sample of this issue: https://github.com/koharubiyori/TrayIconTest. I just simply created a blank winui3 app in Visual Studio and added some code following TrayIcon.xaml and App.xaml.cs in WinUI - Windowless. It can work properly but if <XamlUICommand x:Key="unused" /> in TrayIcon.xaml is commented out, the program will crash. My app uses tray icon and its context menu as the only UI, so I didn't try the sample with window.

@TWiesendanger
Copy link

TWiesendanger commented Jan 29, 2025

@koharubiyori I can make your sample TrayIconTest work if I add the trayicon in the codebehind and making sure that there is a static variable for it. Soemthing like this:

using Microsoft.UI.Xaml;

namespace TrayIconTest
{
    public partial class App : Application
    {
        private TrayIcon? _trayIcon;

        public App()
        {
            this.InitializeComponent();
        }

        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            _trayIcon = new TrayIcon();
        }
  }
}

and the TrayIcon is something like this:

  public TrayIcon()
  {
          
      var trayIcon = new TaskbarIcon
      {
          ContextMenuMode = ContextMenuMode.SecondWindow,
          IconSource = new BitmapImage(new Uri("ms-appx:///Assets/InventorReset_128.ico")),
          ToolTipText = "ToolTip",
          Visibility = Visibility.Visible,
          NoLeftClickDelay = false
      };



      var contextMenu = new MenuFlyout
      {
          AreOpenCloseAnimationsEnabled = false
      };

      var closeMenuItem = new MenuFlyoutItem
      {
          Text = "Exit",
      };

      contextMenu.Items.Add(closeMenuItem);

      trayIcon.ContextFlyout = contextMenu;
      trayIcon.ForceCreate();
  }

@TheRealHuzy
Copy link

@koharubiyori I've tried your sample and I do not know why would it behave like that. I hope that you can work around that by defining it in code behind like @TWiesendanger defined in his sample.

@TheRealHuzy
Copy link

@TWiesendanger Thank you very much master Tobias! Your sample works fine.

@koharubiyori
Copy link
Author

Defining the TrayIcon entirely in the code behind can work, but a new bug occurred after doing so. The container of the context menu seemed smaller than its items when it was first shown. I then tried setting the size of the MenuFlyout, but it had no effect.

Image

@TheRealHuzy
Copy link

@koharubiyori I've tried adding more items and it does seem not to expand to text span initially (on the first click/interaction) but the second interaction and every subsequent one does scale the Flyout size properly.

What I've done to fix this is to define Width in the first item that I add to the context menu. You might have to tweak the value to the specific longest menu item you have but it does work.

var menuItem = new MenuFlyoutItem
{
    Text = "A really long text for menu",
    Width = 200
};

Here is the result.

Image

I've tried dynamically calculating the value with something like this

var max = contextMenu.Items.Max(item => item.Width);
contextMenu.Items[0].Width = max; 

but the main problem is that Width of Items has no initial value.

Image

And events like Opening and Opened do not trigger for some unknown reason to me.

Image

@koharubiyori
Copy link
Author

koharubiyori commented Feb 6, 2025

@TheRealHuzy Thank you so much, this is a good workaround. I found that actualWidth=0 and Width=NaN until the context menu was shown for the first time. I tried manually showing the context menu to initialize its width and getting it by listening for contextMenu.Items[0].Loaded, but the app broke on global::System.Diagnostics.Debugger.Break() when trayIcon.ShowContextMenu(Point.Empty) was called. I couldn't seem to fix it as a beginner in .NET. Regarding Opening and Opened, I encounter the same bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants