无法在辅助监视器中创建WPF窗口

时间:2011-12-02 21:24:36

标签: c# wpf user-interface

我正在努力在应用中实现一些类似Chrome的标签功能,并且在使新实例正确生成时遇到一些麻烦。我已经完成了很多搜索和迭代各种解决方案,但尚未能在第二台显示器上生成新窗口。

这是使用线程:

  1. 打开文件
  2. 将当前标签拖到其他监视器
  3. 新应用程序实例在其中设置了该选项卡,在用户将选项卡拖动到的位置生成。
  4. 断开连接在步骤3中。新实例始终在主监视器上生成。

    所以,有些代码可以扩展问题。

    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和当前覆盖我的集合的appView之间存在某种联系。有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:4)

您是否尝试过使用Winforms Screen.FromControl?请参阅this post

你可以试试this post

appView.SourceInitialized += (_, __) => appView.WindowState = WindowState.Maximized;
appView.Show();
相关问题