关于notifyIcon的C#事件绑定不能正常写入

时间:2011-08-19 14:15:46

标签: c#

我正在为我的公司开发一个桌面应用程序,并且似乎无法使此代码与notifyIcon.MouseClickEvent上的MouseClickEvent绑定一起工作。以下是代码,有关做什么的整体见解会有所帮助。我确实在一个点上有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);
        }

    }
}

1 个答案:

答案 0 :(得分:1)

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