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

aurhwmvo  于 2024-01-04  发布在  其他
关注(0)|答案(6)|浏览(176)

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

  1. public static void openCloseWindow(Window toBeOpen, Window toBeClose)
  2. {
  3. toBeOpen.Show();
  4. makeWindowCenter(toBeOpen);
  5. toBeClose.Close();
  6. }

字符串
在程序的一部分中,我使用了这样的方法:

  1. openCloseWindow(new BestCustomerWindow,this);


因此最终用户可以在按钮上点击几次,并且可以打开许多窗口。
有什么方法可以避免在运行时打开窗口吗?

  • 更多信息:*

让我点击一个按钮,这是打开窗口1。我想:

  • 如果窗口1已关闭,请将其打开。
  • 否则,如果窗口1已打开,则聚焦于窗口1。
jchrr9hc

jchrr9hc1#

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

  1. bool isWindowOpen = false;
  2. foreach (Window w in Application.Current.Windows)
  3. {
  4. if (w is WINDOWNAME)
  5. {
  6. isWindowOpen = true;
  7. w.Activate();
  8. }
  9. }
  10. if (!isWindowOpen)
  11. {
  12. WINDOWNAME newwindow = new WINDOWNAME();
  13. newwindow.Show();
  14. }

字符串

展开查看全部
6kkfgxo0

6kkfgxo02#

阿巴德,
我认为你可以使用Mutex,请参考下面的代码(在App.xaml.cs文件中):

  1. public partial class App : Application
  2. {
  3. [DllImport("user32", CharSet = CharSet.Unicode)]
  4. static extern IntPtr FindWindow(string cls, string win);
  5. [DllImport("user32")]
  6. static extern IntPtr SetForegroundWindow(IntPtr hWnd);
  7. [DllImport("user32")]
  8. static extern bool IsIconic(IntPtr hWnd);
  9. [DllImport("user32")]
  10. static extern bool OpenIcon(IntPtr hWnd);
  11. protected override void OnStartup(StartupEventArgs e)
  12. {
  13. bool isNew;
  14. var mutex = new Mutex(true, "My Singleton Instance", out isNew);
  15. if (!isNew)
  16. {
  17. ActivateOtherWindow();
  18. Shutdown();
  19. }
  20. }
  21. private static void ActivateOtherWindow()
  22. {
  23. var other = FindWindow(null, "MainWindow");
  24. if (other != IntPtr.Zero)
  25. {
  26. SetForegroundWindow(other);
  27. if (IsIconic(other))
  28. OpenIcon(other);
  29. }
  30. }
  31. }

字符串

展开查看全部
yuvru6vn

yuvru6vn3#

这段代码将完全满足您的要求:
只需存储对话框对象并检查它是否已经在showWindow中创建。
使用windows Closed事件清除对对话框对象的引用。

  1. AddItemView dialog;
  2. private void showWindow(object obj)
  3. {
  4. if ( dialog == null )
  5. {
  6. dialog = new AddItemView();
  7. dialog.Show();
  8. dialog.Owner = this;
  9. dialog.Closed += new EventHandler(AddItemView_Closed);
  10. }
  11. else
  12. dialog.Activate();
  13. }
  14. void AddItemView_Closed(object sender, EventArgs e)
  15. {
  16. dialog = null;
  17. }

字符串
请注意这个问题已经在这里问过了

展开查看全部
vc6uscn9

vc6uscn94#

我写了一个基于@ rand-towey answer的快速函数

  1. void OpenNewOrRestoreWindow<T>() where T : Window, new()
  2. {
  3. bool isWindowOpen = false;
  4. foreach (Window w in Application.Current.Windows)
  5. {
  6. if (w is T)
  7. {
  8. isWindowOpen = true;
  9. w.Activate();
  10. }
  11. }
  12. if (!isWindowOpen)
  13. {
  14. T newwindow = new T();
  15. newwindow.Show();
  16. }
  17. }

字符串
用途OpenNewOrRestoreWindow<SomeWindow>();

展开查看全部
js81xvg6

js81xvg65#

10年后,但希望对某人有用:

  1. var winname = "MYWINDOW";
  2. //Validate
  3. bool iswinopen = false;
  4. foreach (Window w in Application.Current.Windows)
  5. {
  6. if (w.Name != winname) continue;
  7. iswinopen = true;
  8. w.Activate();
  9. break;
  10. }
  11. if (iswinopen) return;
  12. //Set
  13. var window = new Window() { Name = winname };
  14. window.ShowInTaskbar = true;
  15. window.Owner = Application.Current.MainWindow;
  16. window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  17. window.WindowState = WindowState.Normal;
  18. window.WindowStyle = WindowStyle.SingleBorderWindow;
  19. window.Content = new MyView() { DataContext = this }; //im use usercontrol as view with viewmodel here
  20. window.Show();

字符串

展开查看全部
bf1o4zei

bf1o4zei6#

只是用

  1. tobeopen.ShowDialog();

字符串
例如,我的窗口类名称是“Window 1”,使用“Window1.ShowDialog();”
如果Windows已经打开,它不会打开,它会发出警报声。

相关问题