Java教程

WPF推荐的单例应用程序

本文主要是介绍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推荐的单例应用程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!