notifyicon没有提高点击事件和contextmenustrip没有显示

时间:2015-11-25 14:49:56

标签: c# wpf wcf notifyicon contextmenustrip

我有一个问题,即notifyicon显示正常,并显示其气泡,但点击时不会引发其点击事件。

它应该显示其contextmenustrip,但是如果我在代码中添加contextmenustrip.show();,它只显示菜单的阴影,而不是菜单本身。

该函数是WPF应用程序的一部分,并通过WCF服务应用程序中的命名管道进行调用。

代码,如果您需要它:

在服务应用中:

public partial class Service1 : ServiceBase
    {
        ChannelFactory<ILicenseWatchingServiceUIHost> pipeFactory;
        ILicenseWatchingServiceUIHost LWSProxy;


        public Service1()
        {
            InitializeComponent();    
        }

        #region service states

        protected override void OnStart(string[] args)
        {
            pipeFactory = new ChannelFactory<ILicenseWatchingServiceUIHost>(
                new NetNamedPipeBinding(),
                new EndpointAddress("net.pipe://localhost/LWSPipe"));
            LWSProxy = pipeFactory.CreateChannel();
            Run();
        }

        //...

        private void Run()
        {
            //...

            LWSProxy.Execute();
        }
    }

在WPF应用程序中创建服务器:

public partial class App : Application
    {
        ServiceHost host = new ServiceHost(typeof(LicenseWatchingServiceUserInterface), new Uri[] { new Uri("net.pipe://localhost") });

        public App()
        {
            StartServer();
        }

        private void StartServer()
        {
            host.AddServiceEndpoint(typeof(ILicenseWatchingServiceUIHost), new NetNamedPipeBinding(), "LWSPipe");
            host.Open();

            BackgroundWorker worker = new BackgroundWorker();
            System.Windows.Threading.Dispatcher dispatcher = this.Dispatcher;
        }
    }

最后:

public class LicenseWatchingServiceUserInterface : ILicenseWatchingServiceUIHost
    {
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.NotifyIcon notifyIcon;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip;

        void Execute(){
        //...
        contextmenustrip.show(); //does not work
        }

        //does not get raised
        private void notifyIcon_Click(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                    contextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
                }
                else if (e.Button == MouseButtons.Left)
                {
                    Execute();
                }
            }
        }

        void Init()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LicenseWatchingServiceUserInterface));

            this.notifyIcon = new System.Windows.Forms.NotifyIcon();
            this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip();

            // 
            // notifyIcon
            // 
            this.notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
            this.notifyIcon.Icon = iconLoading;
            this.notifyIcon.Visible = true;
            //clickhandler is in fact wired up
            this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon_Click);
            // 
            // contextMenuStrip
            // 
            this.contextMenuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.settingsToolStripMenuItem,
            this.openLicenseManagerToolStripMenuItem,
            this.closeToolStripMenuItem});
            this.contextMenuStrip.Name = "contextMenuStrip";
            this.contextMenuStrip.Size = new System.Drawing.Size(234, 128);
            //
            // contextmenustrip items...
            //
        }
        //...
    }

1 个答案:

答案 0 :(得分:1)

想出来:

LWSProxy.Execute();正在后台线程中创建LicenseWatchingServiceUserInterface类的实例,这导致了问题。必须在主线程中创建notifyicon,否则事件处理程序将无法工作。我的解决方案是一个帮助类(只是把它放在class App下面):

public class LicenseWatchingServiceUICreator : ILicenseWatchingServiceUIHost
    {

        public void Execute(List<LicenseInfoContainerExpiring> elcs, List<LicenseInfoContainerUntrusted> ulcs)
        {
            System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => 
            {
                LicenseWatchingServiceUserInterface LWSUI = new LicenseWatchingServiceUserInterface(elcs, ulcs); 
            }));
        }

    }

稍微调整一下,这个类的execute方法将通过管道调用,它将在主线程中创建我的类的实例。现在我的UI像往常一样工作。