避免显示或打开多个实例并显示当前实例 [英] Avoiding Showing or Opening More than One instance and Showing the Current One

查看:195
本文介绍了避免显示或打开多个实例并显示当前实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我开发了一个简单的应用程序,并且在我的应用程序中,用户可以从多个位置访问该表单,这有时会使一个表单多次打开.
因此,我想使每个表单都被打开或显示出来,或者仅显示其中一个实例.
有人可以帮我做..吗?
感谢高级

hi guys,
i develop a simple application and in my application user can reach to the form from more than one place , and that makes some times one form is opened more than one time
So i want to Make each Form Is opened or Showed only one instance from it created
can any one help me in doing that ..?
thanks for advanced

推荐答案

您可以在Web上使用WPF在单个实例"或单个应用程序实例"下搜索许多方法.我正在使用.NET远程处理的一种非常狡猾且紧凑的方法.我不确定是否可以在不发表另一篇文章的情况下进行解释,但是我会尽力的.

请在这里查看我过去的解决方案:
在已经打开的程序中打开文件 [在已经打开的程序中打开文件 [
There is a number of methods you can search on Web under "single-instance" or "single application instance" with WPF. I''m using a pretty cunning and compact method using .NET remoting. I''m not sure I can explain it without publishing yet another article, but I''ll try.

Please see my past solutions here:
Open File in already Open Program[^],
Open File in already Open Program[^].

—SA


似乎您应该实现一种将表单分发给所有类的机制,以便能够显示它.如果您分发引用指针,则将始终获得该表单的相同实例.

假设您有一个Form.您可以在主类中创建该实例.然后,将其传递给简单应用程序的构造函数中或作为属性传递给子类(仅供参考:实际上,您实际上只是将引用指针复制到该表单).然后,您使用ShowHide.现在重要的是,通过处理Closing事件并将CancelEventArgsCancel属性设置为true,然后调用Hide来覆盖窗体的关闭.
It seems you should implement a mechanism to distribute your form to all the classes how should be able to show it. If your distribute the reference pointer, you will always get the same instance of the form.

So say you have a Form. You can create a instance of that in your main class. Then you pass it down to your sub-classes either in the constructor for simple apps, or as a property (FYI: you actually will then just copy the reference pointer to that form). You then use Show and Hide. It is now important that you override the closing of the form by handling the Closing event and setting the Cancel property of the CancelEventArgs to true, and calling Hide instead.


您可以使用单例设计模式 [
You can use the singleton design pattern[^] for that. That pattern ensures that there''s only one instance of a class.

example code for a WPF Window:
public sealed partial class SingleWindow : Window {
    private static SingleWindow instance;
    private bool isMainExit=false;
    private SingleWindow() {
        InitializeComponent();
        App.Current.MainWindow.Closed+=new EventHandler(MainWindow_Closed);
    }
    public static SingleWindow Instance {
        get {
            if (instance == null) {
                instance = new SingleWindow();
            }
            return instance;
        }
    }
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
        if (!isMainExit) {
            this.Hide();
            e.Cancel = true;
        }
        base.OnClosing(e);
    }
    private void MainWindow_Closed(object sender, EventArgs e) {
        this.isMainExit=true;
        this.Close();
    }
}



关闭表单/窗口后,表单/窗口将进入无法使用的状态(我想直到垃圾收集器将其完全删除为止).因此,您需要检查表单/窗口是否已关闭.表单被处置,因此您检查IsDisposed.我无法立即为WPF窗口找到类似的机制,因此我实现了一种解决方法.如果有人知道更优雅的解决方案,请纠正我. :-)

用WPF代码替换WinForms代码



After you close a form/window that form/window enters a state where you can''t use it anymore (until the garbage collecter removes it entirely, I suppose). Therefore you need to check wether the form/window has been closed. Forms get disposed so you check for IsDisposed. I couldn''t immediately find a similar mechanism for a WPF Window so I implemented a workaround. If anybody knows a more elegant solution, please correct me. :-)

edit: replaced WinForms code by WPF code


这篇关于避免显示或打开多个实例并显示当前实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆