Skip to content

[Feature request] NotifyIcon 支持无窗口时运行 #748

Closed

Description

@GiantappMan

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
App.xaml 不指定主窗口的情况下,直接启动托盘。目前实现不了。

Describe the solution you'd like
A clear and concise description of what you want to happen.
以下代码我测试了,可以显示icon,但是右键无法弹出contextMenu。

    public partial class App : Application
    {
        NotifyIcon _notifyIcon = null;
        protected override void OnStartup(StartupEventArgs e)
        {
            string apptEntryDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string iconPath = Path.Combine(apptEntryDir, "Assets\\Img\\logo.ico");

            ContextMenu menu = new ContextMenu();
            menu.Items.Add(new MenuItem() { Header = "主界面" });
            menu.Items.Add(new MenuItem() { Header = "退出", Command = ControlCommands.ShutdownApp });

            _notifyIcon = new NotifyIcon()
            {
                Token = "token",
                Icon = new BitmapImage(new Uri(iconPath, UriKind.Absolute)),
                ContextMenu = menu
            };

            _notifyIcon.Init();
            base.OnStartup(e);
        }
    }

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

分析源码,我发现用IsLoaded过滤了事件处理。这句话屏蔽即可正常运行,不知道这样设计是否有什么特殊原因。

       private IntPtr Callback(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
        {
            //if (IsLoaded)

Additional context
Add any other context or screenshots about the feature request here.

Activity

  1. GiantappMan commented on Mar 29, 2021

    @GiantappMan
    Author

    能更新一个nuget版本么,感谢

  2. GF-Huang commented on May 7, 2021

    @GF-Huang
    Member

    Same issue. Is it fixed and release to NuGet?

  3. GF-Huang commented on May 8, 2021

    @GF-Huang
    Member

    有无可能直接在 app.xaml 中定义 NotifyIcon 而不用手写 C# 代码去定义?

  4. added a commit that references this issue on Mar 28, 2024
    ad32fc8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址