在辅助监视器中创建WPF窗口时出现问题

本文关键字:窗口 问题 WPF 创建 监视器 | 更新日期: 2023-09-27 18:16:36

我正在一个应用程序中实现一些类似chrome的选项卡功能,并且我在获得新实例正确生成时遇到了一些麻烦。我已经在各种解决方案上做了相当多的搜索和迭代,但还没有能够在第二个监视器上生成一个新窗口。

下面是use线程:

  1. 打开文件
  2. 将当前选项卡拖动到其他监视器
  3. 新的应用实例会在用户拖动标签到的位置生成。

断开连接在步骤3中。新实例总是在Primary监视器上生成。

那么,一些代码来展开这个问题。

namespace app {
    public class AppView {
      public void OpenInNewWindow()
      {
        // Create a new viewmodel
        var appViewModel = new AppVM();
        //// On my machine this returns the correct screen "DISPLAY2".  The Top and Left properties are 0 and 1680, respectively.
        var targetScreen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
        ////So we can set the position of the new view
        var appView = new AppView(appViewModel);
        //This seats the currently selected data tab inside the new AppViewModel
        RelocateSelectedViewModel(appViewModel);
        appView.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
        appView.Top = targetScreen.WorkingArea.Top;
        appView.Left = targetScreen.WorkingArea.Left;
        appView.Show();
        // Have to maximize after we Show() or it won't appera on secondary monitors according to THE INTERNET!
        appView.WindowState = System.Windows.WindowState.Maximized;
        appView.Focus();            
      }
    }
}

我想我应该提一下,我得到第二个屏幕没有问题。上面代码中的targetScreen正确地找到了我想要的屏幕,并且新窗口的Top和Left值分别被正确地设置为0和1680。只是AppView.Show()命令(实际上是window . show())在主屏幕上创建了窗口。

我已经把同样的代码到一个独立的项目,它已经工作了,这让我相信有某种联系在我的新appView和当前的一个是覆盖我的集合在这里。以前有人遇到过这个问题吗?

在辅助监视器中创建WPF窗口时出现问题

你试过使用Winforms Screen.FromControl吗?请看这篇文章

你可以从这篇文章中尝试这个hack:

appView.SourceInitialized += (_, __) => appView.WindowState = WindowState.Maximized;
appView.Show();