首页 > 代码库 > WPF开发经验

WPF开发经验

1.应用程序中正在运行线程时,System.Windows.Application.Current.Shutdown();关闭应用程序有异常,使用下边方法。
close的时候子线程还处于active状态吧。。完全退出用
System.Environment.Exit(System.Environment.ExitCode);
2.托盘窗口

this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "running in background";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "running in background";
            this.notifyIcon.Icon = new System.Drawing.Icon(DirectoryManager.AppPath + "\\UpDownload_48x48.ico");
            this.notifyIcon.Visible = true;
            this.Icon = new BitmapImage(new Uri(@"UpDownload_48x48.ico", UriKind.Relative));
            //打开菜单项
            System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
            open.Click += new EventHandler(Show);
            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
            exit.Click += new EventHandler(Close);
            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
            {
                if (e.Button == MouseButtons.Left) this.Show(o, e);
            });

private NotifyIcon notifyIcon;
        private void Show(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private void Hide(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Close(object sender, EventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode); 
            //System.Windows.Application.Current.Shutdown();
        }

 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
                e.Cancel = true;
                Hide();

        }

3.判断应用程序是否开启状态

//获取欲启动进程名
            string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            ////获取版本号
            //CommonData.VersionNumber = Application.ProductVersion;
            //检查进程是否已经启动,已经启动则显示报错信息退出程序。
            if (System.Diagnostics.Process.GetProcessesByName(strProcessName).Length > 1)
            {

                MessageBox.Show("MetadataSync running in background!", "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                Application.Current.Shutdown();
                return;
            }

WPF开发经验