我有一個小問題,並試圖解決它現在近6至8小時,但我沒有找到任何匹配的答案。我是一個WPF的新手,所以請給我指出我犯的任何錯誤。從WinForms轉換MultipleMonitor應用程序到WPF
起初我已經在我的App.xaml.cs如下:
namespace WpfVideowand
{
public partial class App : Application
{
...
private void Application_Startup(object sender, StartupEventArgs e)
{
foreach (System.Windows.Forms.Screen MyScreen in System.Windows.Forms.Screen.AllScreens)
{
List<string> MyStrings = Xml.GetScreens(i);
if (MyStrings[1] == "true")
{
OpenWindow(MyScreen, MyStrings[0], i);
}
i++;
Shelf MyShelf = new Shelf(MyScreen, i, MyStrings[0]);
MyShelf.Show();
}
}
private void OpenWindow(System.Windows.Forms.Screen myScreen, string configName, int screenNumber)
{
Shelf NewShelf = new Shelf(myScreen, screenNumber, configName);
}
}
}
而且Shelf.xaml.cs裏面,它看起來是這樣的:
namespace WpfVideowand
{
public partial class Shelf : Window
{
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
System.Windows.Forms.Screen _Screen { get; set; }
...
public Shelf(System.Windows.Forms.Screen myScreen, int screenNumber, string configName)
{
InitializeComponent();
_Screen = myScreen;
ShowOnMonitor(screenNumber);
...
}
private void ShowOnMonitor(int screenNumber)
{
System.Windows.Forms.Screen[] ScreenArray;
ScreenArray = System.Windows.Forms.Screen.AllScreens;
int XCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Left);
this.Left = XCoord;
int YCoord = Convert.ToInt32(ScreenArray[screenNumber].Bounds.Top);
this.Top = XCoord;
IntPtr active = GetActiveWindow();
System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).Name = "Monitor" + screenNumber.ToString();
System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(window => new WindowInteropHelper(window).Handle == active).WindowState = WindowState.Maximized;
}
...
}
}
上面所描述的工作方式在Windows窗體應用程序罰款。在WPF中,我遇到了問題,我得到了錯誤消息,那個矩形(窗口)將沒有Top或Left屬性。
我甚至嘗試了一些其他的方式,比如用
System.Windows.Forms.Screen _screen = System.Windows.Forms.Screen.FromControl(this);
創建一個對象,這將有.TOP和。左。但是我得到的消息是,我無法將Shelf對象轉換爲System.Windows.Forms.Control。 任何人的建議,我怎麼能讓我的屏幕出現在顯示器上它應該是什麼?