简体   繁体   中英

WPF NotifyIcon ContextMenu with hardcode taskbar

I try to implement this solution for my 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. And it is work good. I see my notifyicon near the windows clock. 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

Any ideas?

ContextMenuStrip replaces ContextMenu and adds functionality.

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. in the Closed event handler of the Window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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