c# notifyIcon上的事件绑定不工作

本文关键字:绑定 工作 事件 notifyIcon | 更新日期: 2023-09-27 18:06:02

我正在为我的公司开发一个桌面应用程序,似乎无法让这段代码像notifyicon . mousecickevent绑定一样工作。下面是代码,全面了解该做什么会有所帮助。我曾经有过Program: ApplicationContext,然后改了Program: Form。这是一个愚蠢的新手错误,但我只是不做太多的c#编程,已经战斗/阅读代码大约两天了,现在似乎不能弄清楚这个问题。请帮我少吸一点。: -)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.IO.Pipes;
using System.Security.Principal;
using System.Xml.Serialization;
using System.IO;
using System.Collections;
namespace ShipItClient
{
    public class Program : Form
    {

        private NotifyIcon notifyIcon;
        private Program()
        {
            SetupTray();
            ListenForSignal();
        }
        protected void notifyIcon_Click(object Sender, MouseEventArgs e)
        {
            MessageBox.Show("Double Click");
            CustomQuoteForm quoteForm = new CustomQuoteForm();
            quoteForm.Show();
        }
        private void SetupTray()
        {
            // Create the NotifyIcon.
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.MouseClick += new MouseEventHandler(notifyIcon_Click);
            notifyIcon.Icon = ShipItClient.Properties.Resources.ShipIt4Sage;
            notifyIcon.Text = "ShipIt - Shipping Rate Quotes on Demand";
            notifyIcon.Visible = true;
        }
        private void ListenForSignal()
        {
            String username = WindowsIdentity.GetCurrent().Name;
            String pipeName = "shipit_" + username.Split('''')[1];
            NamedPipeClientStream pipeClient =
                    new NamedPipeClientStream(".", pipeName,
                        PipeDirection.InOut, PipeOptions.None);
            pipeClient.Connect();
            while (pipeClient.IsConnected)
            {
                if (pipeClient.ReadByte() == 1)
                {
                    string[] shipit_files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "XML_*.xml");
                    foreach (String file in shipit_files)
                    {
                        string xml = File.ReadAllText(file);

                        XmlSerializer serializer = new XmlSerializer(typeof(ShipIt));
                        ShipIt ship_it = (ShipIt)serializer.Deserialize(new StringReader(xml));
                        //Get ShipIt Quote Window
                        RateGrid rateGrid = new RateGrid();
                        //rateGrid.Visibility = System.Windows.Visibility.Visible;
                        File.Delete(file);
                    }
                }
            }
        }
        private void GetCarrierRates(ShipIt ship_it)
        {
            FedExRates fedexRates = new FedExRates(ShipItClient.Properties.Settings.Default.FedEx_Account_Number, ShipItClient.Properties.Settings.Default.FedEx_Meter_Number, ShipItClient.Properties.Settings.Default.FedEx_Service_Key, ShipItClient.Properties.Settings.Default.FedEx_Service_Password);
            ArrayList rates = fedexRates.GetRate(ship_it);   
        }
        [STAThread]
        public static void Main()
        {
            Program pg = new Program();
            Application.Run(pg);
        }
    }
}

c# notifyIcon上的事件绑定不工作

通知图标的事件由消息泵(Application.Run())处理,但是您没有消息泵。由于您的命名管道调用将阻塞调用,我建议您在单独的线程中调用ListenForSignal。注意,如果这样做,则需要使用InvokeRequiredInvoke来更新GUI。