Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从类库检查WPF窗口是否打开_C#_Wpf - Fatal编程技术网

C# 如何从类库检查WPF窗口是否打开

C# 如何从类库检查WPF窗口是否打开,c#,wpf,C#,Wpf,我正在从类库打开一个WPF窗口。如果WPF窗口已经打开,如何从类库中进行检查?对System.Windows.Application.Current.Windows的任何引用都会产生此错误: 对象引用未设置为对象的实例 注意:我从一个WPF项目创建了类库,我将输出类型更改为类库。我尝试引用System.Windows,但无法这样做 但强制执行只有一个流程实例正在运行的规则是一项有趣的任务。在Win32上,还有许多其他方法可以对算法进行编码。它将检测应用程序是否已经在运行。如果检测到,它将把该应用

我正在从类库打开一个WPF窗口。如果WPF窗口已经打开,如何从类库中进行检查?对
System.Windows.Application.Current.Windows的任何引用都会产生此错误:

对象引用未设置为对象的实例


注意:我从一个WPF项目创建了类库,我将输出类型更改为类库。

我尝试引用System.Windows,但无法这样做

但强制执行只有一个流程实例正在运行的规则是一项有趣的任务。在Win32上,还有许多其他方法可以对算法进行编码。它将检测应用程序是否已经在运行。如果检测到,它将把该应用程序带到前台(如果最小化)


我已尝试引用System.Windows,但无法这样做

但强制执行只有一个流程实例正在运行的规则是一项有趣的任务。在Win32上,还有许多其他方法可以对算法进行编码。它将检测应用程序是否已经在运行。如果检测到,它将把该应用程序带到前台(如果最小化)


窗口是否处于相同的过程中?如果您只想显示窗口的一个实例,是否考虑过将其设置为singleton?我没有查看singleton,我会这样做。谢谢你的建议。我还更改了我的设计,这样我就不需要使用WPF窗口,而是使用WinForm。我在检查winform实例时没有遇到任何问题。窗口是否在同一进程中?如果您只想显示窗口的一个实例,是否考虑过将其设置为singleton?我没有查看singleton,我会这样做。谢谢你的建议。我还更改了我的设计,这样我就不需要使用WPF窗口,而是使用WinForm。我在检查winform实例时没有任何问题。
using System.Diagnostics;
using System.Runtime.InteropServices;
public class OnlyOneWindow
{
    [DllImport("user32.dll")] private static extern 
        bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")] private static extern 
        bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] private static extern 
        bool IsIconic(IntPtr hWnd);

    private const int SW_HIDE = 0;
    private const int SW_SHOWNORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;
    private const int SW_SHOWNOACTIVATE = 4;
    private const int SW_RESTORE = 9;
    private const int SW_SHOWDEFAULT = 10;

    public OnlyOneWindow()
    {
        // get the name of our process
        string proc=Process.GetCurrentProcess().ProcessName;
        // get the list of all processes by that name
        Process[] processes=Process.GetProcessesByName(proc);
        // if there is more than one process...
        if (processes.Length > 1)
        {
            // Assume there is our process, which we will terminate, 
            // and the other process, which we want to bring to the 
            // foreground. This assumes there are only two processes 
            // in the processes array, and we need to find out which 
            // one is NOT us.

            // get our process
            Process p=Process.GetCurrentProcess();
            int n=0;        // assume the other process is at index 0
            // if this process id is OUR process ID...
            if (processes[0].Id==p.Id)
            {
                // then the other process is at index 1
                n=1;
            }
            // get the window handle
            IntPtr hWnd=processes[n].MainWindowHandle;
            // if iconic, we need to restore the window
            if (IsIconic(hWnd))
            {
                ShowWindowAsync(hWnd, SW_RESTORE);
            }
            // bring it to the foreground
            SetForegroundWindow(hWnd);
            // exit our process
            return;
        }
        // ... continue with your application initialization here.
    }
}