WPF推荐的单例应用程序
2021/8/4 14:06:29
本文主要是介绍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推荐的单例应用程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南