【发布时间】:2012-06-28 13:27:18
【问题描述】:
我的代码并不是那么相关,只是为了给你背景,我有一个方法可以像这样打开一个窗口 messageWindow 的实例;
private void SetMessagePosition(Controls.Button btn, string text)
{
messageWindow = new messageWindow(text);
relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));
messageWindow.Left = relativePoint.X + this.Left;
messageWindow.Top = relativePoint.Y + this.Top;
messageWindow.Show();
}
但是我想看看我是否可以使用这种方法打开其他窗口。这显然意味着将我要打开的新窗口的名称作为参数传递给它。我的问题是,怎么做?我试过像这样传递参数;
private void SetMessagePosition(Window newWin, Controls.Button btn, string text))
{
newWin = new newWin(text);
...
其中newWin = 我要打开的窗口类型。但显然new newWin 部分会引发错误,因为VS 不知道名为newWin 的窗口。
我知道你的第一个想法可能是,为什么不在调用这个方法之前实例化窗口,然后我可以一起跳过这行。好吧,这个方法实际上是在打开时设置新窗口相对于父窗口的位置,所以,我之前不能设置它的位置。
我想尝试的另一件事是;
List<Window> winList;
List<Type> winListType;
winList.Add(window1);
winList.Add(window2);
winList.Add(window3);
winListType.Add(Window1);
winListType.Add(Window2);
winListType.Add(Window3);
SetMessagePosition(winList[2], winListType[2], btn1, "Yes");
private void SetMessagePosition(Window newWin, Type newWinType, Controls.Button btn, string text))
{
newWin = new newWinType(text);
...
但newWinType 不喜欢传递Type 而不是变量,即使它是Type 的列表。如果有人知道这样做的方法/解决方法,我会印象深刻。
【问题讨论】:
-
为了更灵活,我建议使用界面而不是窗口和类型
-
@HichemC 抱歉,但我对接口一无所知。现在将阅读它们。
标签: c# wpf parameters window