如何创建恢复时试图打开另一个实例打开的窗口中单一实例WPF应用程序? [英] How to create single instance WPF Application that restores the open window when an attempt is made to open another instance?

查看:224
本文介绍了如何创建恢复时试图打开另一个实例打开的窗口中单一实例WPF应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起它的标题是很难理解的。我不知道如何字呢。

Sorry it the title is hard to understand. I wasn't sure how to word it.

我有只应允许运行每个用户会话一个实例的应用程序。
如果用户点击重新启动应用程序,我想带一个已经进行对焦。

I have an application that should only be allowed to run one instance per user session. If the user clicks to launch the application again, I want to bring the one already to focus.

窗口可能将有能见度要崩溃了。

The window will likely have Visibility to collapsed.

如果它是可见的,我知道我可以使用

If it's visible I know I can use

if (IsIconic(hWnd))
{
    ShowWindowAsync(hWnd, swRestore);
}

SetForegroundWindow(hWnd);



但如果是折叠的窗口,有没有为我把它带回可见的一种方式?

but if the window is collapsed, is there a way for me to bring it back to visible?

推荐答案

您正在寻找的的 互斥。这是非常复杂的,但幸运的是Singleton模式已经被广泛的讨论。有它几篇好文章,但你可以找到在 C#一个很好的实现它的.NET单实例应用程序理智自由编码网站上的页面。从所链接的页面:

You're looking for the Mutex Class. It's pretty complicated, but luckily the Singleton Pattern has been widely discussed. There are several good articles on it, but you can find a good implementation of it in the C# .NET Single Instance Application page on the Sanity Free Coding website. From the linked page:

static class Program {
    static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
    [STAThread]
    static void Main() {
        if(mutex.WaitOne(TimeSpan.Zero, true)) {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            mutex.ReleaseMutex();
        } else {
            MessageBox.Show("only one instance at a time");
        }
    }
}

现在你可能想知道如何在一个WPF应用程序在的方法,对吗?那么有,你必须做一些事情,但它并不困难。请参阅编写自定义Main()方法对于WPF应用程序的文章,其中解释了这个细节。那篇文章:

Now you're probably wondering how to have a Main method in a WPF Application, right? Well there's a few things that you have to do, but it's not difficult. See the Writing a custom Main() method for WPF applications article which explains this in detail. From that article:

您基本上需要从应用程序定义改变应用程序的构建行动页面,创建一个调用构造函数的InitializeComponent,并最终调用应用程序的运行方法重载的一个写你的Main()。 ......别忘了,还有,除去从App.xaml中的的StartupUri,否则窗口的另一个副本将出现(除非你得到一个错误,因为URI指向一个不存在的XAML资源)。

You basically need to change the application’s build action from "Application Definition" to "Page", create a constructor that calls "InitializeComponent", and write your Main() by eventually calling one of the application’s "Run" method overloads. ... Don’t forget, also, to remove the "StartupUri" from the App.xaml, otherwise another copy of window will show up (unless you get an error because the URI points to a non existing XAML resource).

因此,通过合并这两种资源,我们可以看到,你的 App.xaml.cs 文件应该是这个样子:

So by amalgamating these two resources, we can see that your App.xaml.cs file should look something like this:

public partial class App : Application
{
    private static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
    private static MainWindow mainWindow = null;

    App()
    {
        InitializeComponent();
    }

    [STAThread]
    static void Main()
    {
        if(mutex.WaitOne(TimeSpan.Zero, true)) 
        {
            App app = new App();
            mainWindow = new MainWindow();
            app.Run(mainWindow);
            mutex.ReleaseMutex();
        }
        else
        {
            mainWindow.WindowState = WindowState.Normal;
        }
    }
}

这篇关于如何创建恢复时试图打开另一个实例打开的窗口中单一实例WPF应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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