question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TrayIcon.Menu: NativeMenu. Opening event is never invoked

See original GitHub issue

Describe the bug I’m trying to create a tray icon menu with dynamic menu items. In order to refresh all items right before right-clicking the tray, I’m subscribing to the NeedsUpdate event of the NativeMenu that is assigned to the tray icon. However, that event is never invoked. Same issue with the Opening event; that isn’t raised either.

To Reproduce Steps to reproduce the behavior:

In my App.xaml I’ve declared a TrayIcon:

<TrayIcon.Icons>
    <TrayIcons>
        <TrayIcon 
                  Icon="/Resources/icon_white.ico" 
                  ToolTipText="App" 
                  Clicked="TrayIcon_OnClicked">
            <TrayIcon.Menu>
                <NativeMenu Items="{Binding MenuItems}"
                            NeedsUpdate="NativeMenu_OnNeedsUpdate"/>
            </TrayIcon.Menu>
        </TrayIcon>
    </TrayIcons>
</TrayIcon.Icons>

In App.xaml.cs:

private void NativeMenu_OnNeedsUpdate(object? sender, EventArgs e)
{
    Log.Debug("This line gets never called");
}

Expected behavior Invoke events properly when opening a tray icon menu

Desktop:

  • OS: Arch Linux 5.15.36-1-lts (KDE)
  • Version 0.10.13

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
timuniecommented, Jun 20, 2023

Indeed, looks like Opening-event is never called. However, I found a way to make dynamic menus work. Here is a draft that for sure needs some more love (null-checks, initial population etc.)

public ObservableCollection<string> MenuItems { get; } = new();

public App() : base()
{
    MenuItems.CollectionChanged += (sender, e) =>
    {
        // Add new Items
        foreach (var newItem in e.NewItems ?? Array.Empty<string>())
        {
            TrayIcon.GetIcons(this).First().Menu.Items.Add(new NativeMenuItem((string)newItem));
        }

        // Remove old items
        foreach (var oldItem in e.OldItems ?? Array.Empty<string>())
        {
            var itemToRemove = TrayIcon.GetIcons(this).First().Menu.Items
                .FirstOrDefault(x => ((NativeMenuItem)x).Header == (string)oldItem);

            TrayIcon.GetIcons(this).First().Menu.Items.Remove(itemToRemove);
        }
    };
}

My App.axaml looks like this:

<TrayIcon.Icons>
  <TrayIcons>
    <TrayIcon 
      x:DataType="sandbox:App"
      Icon="test_icon.ico" 
      ToolTipText="App" >
      <TrayIcon.Menu>
        <NativeMenu />
      </TrayIcon.Menu>
    </TrayIcon>
  </TrayIcons>
</TrayIcon.Icons>

And inside a Button-click I do this:

(Application.Current as App)?.MenuItems.Add($"Added Item {i++}");
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to load an application to systemtray using Avalonia
I created a sample WPF application using .NET Framework (so that I was able to reference System.Windows.Forms ) and was able to have...
Read more >
Touch001 Solving Tray Icon and minimalize UI problem on ...
When I started to think about features for my project it came to my mind, that when the user minimalizes the application it...
Read more >
Adobe Flash Platform * Menu basics
Context menus: Context menus open in response to a right-click or command-click on an interactive object in SWF content or a document element...
Read more >
TrayIcon (Java Platform SE 8 )
A TrayIcon can have a tooltip (text), an image, a popup menu, and a set of listeners associated with it ... TrayIcon processes...
Read more >
TrayIcon
The TrayIcon control allows an Avalonia application to display an icon and a native menu in the system tray. It is supported on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found