简体   繁体   English

WPF NotifyIcon ContextMenu 与硬编码任务栏

[英]WPF NotifyIcon ContextMenu with hardcode taskbar

I try to implement this solution for my NotifyIcon.我尝试为我的 NotifyIcon 实施解决方案。 I did it by this way.我是通过这种方式做到的。

    private void InitNotifyIcon()
    {
        tbi = new TaskbarIcon();
        tbi.Icon = Properties.Resource.favIcon;
        tbi.DoubleClickCommand = new NotifyIconCommand(this);
        tbi.ToolTipText = "Double click - open window";
        this.WindowState = WindowState.Minimized;
    }

In my XAML file I didn't write anything.在我的XAML文件中,我什么也没写。 And it is work good.而且效果很好。 I see my notifyicon near the windows clock.我在 windows 时钟附近看到了我的通知图标。 But now I need to realise with right click context menu.但是现在我需要通过右键单击上下文菜单来实现。 And I have a problem.我有一个问题。 ContextMenu is not a part of TaskbarIcon screenshot of the mistake in Xaml But if you will open link of this exemple you will see that ContextMenu must be include to TaskbarIcon ContextMenu不是 TaskbarIcon 的一部分TaskbarIcon中的错误截图但是如果你打开这个例子的链接你会看到ContextMenu必须包含到TaskbarIcon

Any ideas?有任何想法吗?

ContextMenuStrip replaces ContextMenu and adds functionality. ContextMenuStrip替换了 ContextMenu 并添加了功能。

Example use:示例使用:

    private readonly NotifyIcon notifyIcon;
    ...
    notifyIcon = new NotifyIcon
    {
        // Icon is in Project folder with a Build Action of 'Resource'
        Icon = new Icon(Application.GetResourceStream(new Uri(@"pack://application:,,,/MyIcon.ico")).Stream),
        Visible = true,
        ContextMenuStrip = new ContextMenuStrip()
    };
    notifyIcon.ContextMenuStrip = new ContextMenuStrip();
    notifyIcon.ContextMenuStrip.Items.Add("Exit");
    notifyIcon.ContextMenuStrip.Items[0].Click += (o, e) => Close();

Don't forget to Dispose() of the NotifyIcon appropriately eg.不要忘记适当地 Dispose() 的 NotifyIcon 例如。 in the Closed event handler of the Window.在 Window 的 Closed 事件处理程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM