我怎样才能确保只有一个WPF窗口是打开的时间? [英] How can I make sure only one WPF Window is open at a time?

查看:98
本文介绍了我怎样才能确保只有一个WPF窗口是打开的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF窗口,我从一个winform应用程序内启动。我只希望允许一旦WPF窗口的实例是同时打开,而不是警告用户,如果他们试图再次打开它。

我有一个问题,但是尝试搜索该WPF窗口被打开,因为窗口是从一个winform启动。寻找一个WinForm的时候我normaly做的是,我搜索现有的 Application.Current.OpenForms ,当在WPF我搜索<$认为的winform的任何实例C $ C> Application.Current.Windows

我的问题是, System.Windows.Application.Current 为null从一个winform内推出的时候,所以我不能搜索的WPF窗口方法。有没有寻找一个开放的窗口的现有实例没有更好的办法?

我的code:

 如果(System.Windows.Application.Current!= NULL)
                {
                    的foreach(System.Windows.Window在System.Windows.Application.Current.Windows赢)
                    {
                        如果(取胜是frmCaseWpf)
                        {
                            的MessageBox.show(你只能有一个活跃的情况下,开放的时间。,打开案例,
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Stop);

                            win.WindowState = System.Windows.WindowState.Normal;
                            win.Focus();
                            win.Activate();
                            返回;
                        }
                    }
                }
 

解决方案

而不是搜索静态应用程序对象,你可以而不是只跟踪这是你的窗口中,用一个静态变量。只要保持一个变量窗口:

 私有静态frmCaseWpf的openWindow = NULL; //假设你的类名是frmCaseWpf
 

当你创建一个窗口,无论是在初始化程序,或OnLoaded,这取决于你如何想它的工作..:

 部分类frmCaseWpf {
    公共frmCaseWpf {
         this.OnLoaded + = frmCaseWpf_OnLoaded;
    }

    私人无效frmCaseWpf_OnLoaded(对象发件人,RoutedEventArgs E)
    {
         如果(this.openWindow!= NULL)
         {
              //显示消息框,积极this.openWindow,关闭此
         }
         this.openWindow =这一点;
    }
}
 

如果您希望此窗口可重复使用,请务必设置this.openWindow = NULL;当您关闭该窗口,也是如此。

I have a WPF window that I am launching from inside of a winform app. I only want to allow once instance of that WPF window to be open at a time, and not warn that user if they try to open it again.

I am having a problem however trying to search for that WPF window being open because the window is being launched from a winform. What I normaly do is when searching for a winform, I search for any instances of that winform existing in the Application.Current.OpenForms, and when in WPF I search for Application.Current.Windows

The problem I have is that System.Windows.Application.Current is null when launched from inside of a winform, so I can't search for the WPF window that way. Is there any better way of searching for an existing instance of an open window?

My Code:

if (System.Windows.Application.Current != null)
                {
                    foreach (System.Windows.Window win in System.Windows.Application.Current.Windows)
                    {
                        if (win is frmCaseWpf)
                        {
                            MessageBox.Show("You may have only one active case open at a time.", "Open Case",
                                      MessageBoxButtons.OK,
                                      MessageBoxIcon.Stop);

                            win.WindowState = System.Windows.WindowState.Normal;
                            win.Focus();
                            win.Activate();
                            return;
                        }
                    }
                }

解决方案

Instead of searching the static application objects, you could instead just track this within your window, with a single static variable. Just keep a variable in the window:

private static frmCaseWpf openWindow = null; // Assuming your class name is frmCaseWpf

When you create a window, either in the initialize routines, or OnLoaded, depending on how you want it to work..:

partial class frmCaseWpf {
    public frmCaseWpf {
         this.OnLoaded += frmCaseWpf_OnLoaded;
    }

    private void frmCaseWpf_OnLoaded(object sender, RoutedEventArgs e)
    {
         if (this.openWindow != null)
         {
              // Show message box, active this.openWindow, close this
         }
         this.openWindow = this;
    }
}

If you want this window to be reusable, make sure to set this.openWindow = null; when you close the window, as well.

这篇关于我怎样才能确保只有一个WPF窗口是打开的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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