避免打开同一个窗口两次 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18244196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:26:38  来源:igfitidea点击:

Avoid opening same window twice WPF

c#wpfxamlwindow

提问by Babak.Abad

I'm writing a program using WPF(C#). I use method like this to open and close windows:

我正在使用 WPF(C#) 编写程序。我使用这样的方法来打开和关闭窗口:

public static void openCloseWindow(Window toBeOpen, Window toBeClose)
{
    toBeOpen.Show();

    makeWindowCenter(toBeOpen);

    toBeClose.Close();
}

In part of program I use this method like this:

在程序的一部分中,我像这样使用这种方法:

openCloseWindow(new BestCustomerWindow,this);

so it is possible to end-user click several times on a button and many windows can open.

因此,最终用户可以在一个按钮上多次单击,并且可以打开许多窗口。

is there any way to avoid opening a windows while it is running?

有没有办法避免在运行时打开窗户?

for more information:

想要查询更多的信息:

lets I click on a button which is open window1. I want to:

让我点击一个打开 window1 的按钮。我想要:

  • if window1 is closed, open it.
  • else if window1 is opened, focus on window1.
  • 如果 window1 已关闭,请将其打开。
  • 否则,如果 window1 已打开,则将焦点放在 window1 上。

回答by Rhys Towey

Replace WINDOWNAMEwith the name of the desired Window:

替换WINDOWNAME为所需窗口的名称:

bool isWindowOpen = false;

foreach (Window w in Application.Current.Windows)
{
    if (w is WINDOWNAME)
    {
        isWindowOpen = true;
        w.Activate();
    }
}

if (!isWindowOpen)
{
    WINDOWNAME newwindow = new WINDOWNAME();
    newwindow.Show();
}

回答by Mark Yu

Abad,

阿巴德,

I think you can use Mutex, please refer below code(in App.xaml.cs file):

我认为您可以使用互斥锁,请参考以下代码(在 App.xaml.cs 文件中):

public partial class App : Application
{
    [DllImport("user32", CharSet = CharSet.Unicode)]
    static extern IntPtr FindWindow(string cls, string win);
    [DllImport("user32")]
    static extern IntPtr SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32")]
    static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32")]
    static extern bool OpenIcon(IntPtr hWnd);

    protected override void OnStartup(StartupEventArgs e)
    {
        bool isNew;
        var mutex = new Mutex(true, "My Singleton Instance", out isNew);
        if (!isNew)
        {
            ActivateOtherWindow();
            Shutdown();
        }
    }
    private static void ActivateOtherWindow()
    {
        var other = FindWindow(null, "MainWindow");
        if (other != IntPtr.Zero)
        {
            SetForegroundWindow(other);
            if (IsIconic(other))
                OpenIcon(other);
        }
    }
}

回答by Ofir

This code will do exactly what you are asking for:

此代码将完全满足您的要求:

Just store the dialog object and check whether it's already been created in showWindow.

只需存储对话框对象并检查它是否已经在 showWindow 中创建。

Used the windows Closed event to clear the reference to the dialog object.

使用 windows Closed 事件清除对对话框对象的引用。

AddItemView dialog;

private void showWindow(object obj)
{

    if ( dialog == null )
    {
       dialog = new AddItemView();
       dialog.Show();
       dialog.Owner = this;
       dialog.Closed += new EventHandler(AddItemView_Closed);
    }
    else
       dialog.Activate();
}

void AddItemView_Closed(object sender, EventArgs e)
    {

        dialog = null;
    }

Please notice that this question already asked here

请注意,这里已经问这个问题

回答by HelloWindowsPhone

I wrote a quick function base on @rhys-towey answer

我写了一个基于@rhys-towey 答案的快速函数

    void OpenNewOrRestoreWindow<T>() where T : Window, new()
    {
        bool isWindowOpen = false;

        foreach (Window w in Application.Current.Windows)
        {
            if (w is T)
            {
                isWindowOpen = true;
                w.Activate();
            }
        }

        if (!isWindowOpen)
        {
            T newwindow = new T();
            newwindow.Show();
        }
    }

Usage OpenNewOrRestoreWindow<SomeWindow>();

用法 OpenNewOrRestoreWindow<SomeWindow>();

回答by Srikanth Puli

just use

只是使用

tobeopen.ShowDialog();    

example my window class name is "Window1" use "Window1.ShowDialog();"

例如我的窗口类名称是“Window1”,使用“Window1.ShowDialog();”

if the window is already opened it wont open and it makes alert sound.

如果窗户已经打开,它就不会打开并发出警报声。