WPF推荐的单例应用程序

1.添加程序集 Microsoft.VisualBasic.dll的引用;

//自定义包装application类

 public class SingleInstanceApplicationWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
       public SingleInstanceApplicationWrapper()
       {
           IsSingleInstance = true;
       }

       private WpfApp app;
       protected override bool OnStartup(StartupEventArgs eventArgs)
       {
          
           app = new WpfApp(eventArgs.CommandLine);
           app.Run();
           return false;
       }

       protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
       {
           if(eventArgs.CommandLine.Count>0)
           {
               app.ShowDocument(eventArgs.CommandLine[0]);
           }

       }

    }

2.派生真正的Application

 public class WpfApp:System.Windows.Application
    {
       Dictionary<string, DocumentWindow> dic = new Dictionary<string, DocumentWindow>();
       ReadOnlyCollection<string>  m_args;
       public WpfApp(ReadOnlyCollection<string> args )
       {
           m_args = args;
       }
       public Dictionary<string, DocumentWindow> DocumentDic
       {
           get { return dic; }
           set { dic = value; }
       }
       protected override void OnStartup(System.Windows.StartupEventArgs e)
       {
           base.OnStartup(e);
           MainWindow main = new MainWindow();
           this.MainWindow = main;
           main.Show();
           if (m_args.Count> 0)
           {
               ShowDocument(m_args[0]);
           }
       }

       public void ShowDocument(string msg)
       {
           if(DocumentDic.Keys.Contains(msg))
           {
               DocumentDic[msg].Activate();
           }
           else
           {
               DocumentWindow doc = new DocumentWindow();
               doc.LoadFile(msg);
               doc.Owner = this.MainWindow;
               doc.Show();
               doc.Activate();
           }
         

       }

    }

3.模拟world,一个单应用程序可以打开多个文档,编写文档窗口类

<Window x:Class="WpfApplication14.DocumentWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DocumentWindow" Height="300" Width="300" Loaded="Window_Loaded" Closed="Window_Closed">
    <Grid>
        <TextBox Name="text1"></TextBox>
    </Grid>
</Window>
  public partial class DocumentWindow : Window
    {
        public DocumentWindow()
        {
            InitializeComponent();
        }
        string m_filename;
        public void LoadFile(string filename)
        {
            this.text1.Text = filename;
            m_filename = filename;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WpfApp app = Application.Current as WpfApp;
            if (!app.DocumentDic.Keys.Contains(m_filename))
            {
                app.DocumentDic.Add(m_filename, this);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            WpfApp app = Application.Current as WpfApp;
            app.DocumentDic.Remove(m_filename);
        }

      
    }

4.最后删除app文件,自行添加入口函数类StartUp.cs

public  class StartUp
    {
      [STAThread]
     public static void Main(string[] args)
      {
          SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
          wrapper.Run(args);
      }
    }

WPF推荐的单例应用程序

 

WPF推荐的单例应用程序

上一篇:sublime 左侧的目录sidebar的字体大小修改 windows和ubuntu下


下一篇:PS教大家制作简单漂亮的QQ空间剪影图文教程