有没有办法将 Controls.ContextMenu 转换为 Forms.ContextMenu?

Is there a way to convert a Controls.ContextMenu to a Forms.ContextMenu?

我在 WPF XAML 中定义了一个上下文菜单,如下所示:

<Window.Resources>
    <ContextMenu x:Key="MyMenu">
        <MenuItem Header="{x:Static props:Resources.MenuItem1}"/>
    </ContextMenu>
</Window.Resources>

我使用 System.Windows.Forms.NotifyIcon "myIcon" 作为托盘图标,因为它的设置和使用非常简单,而且似乎没有标准的 MSFT WPF 等效项。不幸的是,我在调用

时遇到了转换异常
this.myIcon.ContextMenu = (ContextMenu)this.Resources["MyMenu"];

因为它们显然不一样 ContextMenu class。有没有办法简单地将 Controls.ContextMenu 转换为 Forms.ContextMenu

我不想通过手动调出我的 XAML 中定义的上下文菜单来处理通知图标的右键单击鼠标事件。原因是我怀疑当用户使用键盘上的上下文菜单键时没有发送右键单击鼠标事件。

否 - 控件适用于完全不同的平台(Winforms 与 WPF)。两者之间没有"conversion"

要么使用支持 WPF 的 a different version of NotifyIcon,要么编写一个 "conversion" 来翻译 WPF 上下文菜单的项目并将它们添加到 Winforms 上下文菜单。

所以,我觉得有人很好奇。我最终实现了一个转换器。

using System;
using System.Drawing;
using System.Windows.Controls;

namespace MyApp
{
    class NotifyIconEx
    {
        #region Data
        private System.Windows.Forms.NotifyIcon _notifyIcon = new System.Windows.Forms.NotifyIcon();
        #endregion // Data

        #region Properties
        public Icon Icon
        {
            get { return _notifyIcon.Icon; }
            set { _notifyIcon.Icon = value; }
        }
        public ContextMenu ContextMenu
        {
            private get { return null; }
            set
            {
                _notifyIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
                foreach (var item in value.Items)
                {
                    if (item is MenuItem)
                    {
                        var menuItem = item as MenuItem;
                        var toolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                        toolStripMenuItem.Click += (s, e) => menuItem.RaiseEvent(new System.Windows.RoutedEventArgs(MenuItem.ClickEvent));
                        toolStripMenuItem.Text = menuItem.Header as string;
                        _notifyIcon.ContextMenuStrip.Items.Add(toolStripMenuItem);
                    }
                    else if (item is Separator)
                    {
                        _notifyIcon.ContextMenuStrip.Items.Add(new System.Windows.Forms.ToolStripSeparator());
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
        }
        public bool Visible
        {
            get { return _notifyIcon.Visible; }
            set { _notifyIcon.Visible = value; }
        }
        #endregion // Properties

        #region API
        public void ShowBalloonTip(int timeout)
        {
            _notifyIcon.ShowBalloonTip(timeout);
        }
        public void ShowBalloonTip(int timeout, string tipTitle, string tipText, System.Windows.Forms.ToolTipIcon tipIcon)
        {
            _notifyIcon.ShowBalloonTip(timeout, tipTitle, tipText, tipIcon);
        }
        #endregion // API
    }
}