开发者

C# Event Bindings on notifyIcon not working write

I am working on a Desktop app for my company and can't seem to get this code to work as far as the MouseClickEvent binding on the notifyIcon.MouseClickEvent. Here is the code, overall insight on what to do would be helpful. I did have Program : ApplicationContext at one point and changed it Program : Form. This is a stupid newbie error, but I just don't do very much C# programming and have been fighting / reading code for about two days now and can't seem to figure this out. Please help me suck less. :-)

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);
        }

    }
}


Events for notification icons are processed by the message pump (Application.Run()), but you have no message pump. Since your named pipe calls will be blocking calls, I would recommend that you call ListenForSignal in a separate thread. Note that if you do this, you will need to use InvokeRequired and Invoke to update the GUI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜