我正在努力在应用程序中实现某些类似于Chrome的标签功能,但在使新实例正确生成时遇到了一些麻烦。我已经做了很多搜索和迭代各种解决方案的工作,但是还没有能够在第二个监视器上产生一个新的窗口。
这是使用线程:
断开连接在步骤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可以正确找到我想要的屏幕,并且新窗口的“上”和“左”值分别正确设置为0和1680。只是AppView.Show()命令(实际上是Window.Show())在主屏幕上创建了窗口。
我已经将相同的代码带到一个独立的项目中,并且它起作用了,这使我相信新的appView与当前的appView之间存在某种联系,这些联系可以覆盖这里的设置。有人遇到过这个问题吗?
最佳答案
您是否尝试过使用Winforms Screen.FromControl
?参见this post。
您可以从this post尝试此技巧:
appView.SourceInitialized += (_, __) => appView.WindowState = WindowState.Maximized;
appView.Show();
关于c# - 无法在辅助监视器中创建WPF窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8362865/