【问题标题】:Avoid opening same window twice WPF避免两次打开同一个窗口 WPF
【发布时间】:2013-08-14 23:51:33
【问题描述】:

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

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

    makeWindowCenter(toBeOpen);

    toBeClose.Close();
}

在部分程序中,我使用这种方法如下:

openCloseWindow(new BestCustomerWindow,this);

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

有什么方法可以避免在运行时打开窗口?

更多信息:

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

  • 如果 window1 已关闭,请打开它。
  • 否则,如果 window1 已打开,则关注 window1。

【问题讨论】:

  • 没关系;在已经打开的窗口上调用 Show() 无效。
  • 我认为您试图避免在已经存在相同类型的窗口时构建新窗口。 stackoverflow.com/questions/17603137/… 之类的东西?

标签: c# wpf xaml window


【解决方案1】:

将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();
}

【讨论】:

    【解决方案2】:

    不好,

    我认为你可以使用 Mutex,请参考以下代码(在 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);
            }
        }
    }
    

    【讨论】:

    • 您似乎使用了编组。很好!但我需要使用安全代码。为您的努力 +1
    【解决方案3】:

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

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

    使用 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;
        }
    

    请注意这个问题已经问过here

    【讨论】:

      【解决方案4】:

      我根据@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();
              }
          }
      

      使用OpenNewOrRestoreWindow&lt;SomeWindow&gt;();

      【讨论】:

      • 谢谢,我试试
      • 这很好,但如果找到窗口,最好打破foreach 循环,您也可以添加bool isDialog = false 参数以允许使用showDialog(),以防用户不允许转到父窗口..
      【解决方案5】:

      只需使用

      tobeopen.ShowDialog();    
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-26
        • 2011-04-12
        • 2021-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多