首页 > Windows开发 > 详细

WPF推荐的单例应用程序

时间:2021-08-04 16:02:17      阅读:38      评论:0      收藏:0      [点我收藏+]

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推荐的单例应用程序

原文:https://www.cnblogs.com/MethodInvoke/p/15098524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!