将窗口/类型作为参数传递

时间:2012-06-28 13:27:19

标签: c# wpf parameters window

我的代码并不是那么相关,只是为了给你背景,我有一个方法可以打开这样的窗口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的列表。如果有人知道这样做的方法/解决方法,我会非常感动。

2 个答案:

答案 0 :(得分:2)

使用这样的界面:

interface IWindow
    {
        int Left
        {
            get;
            set;
        }
        int Right
        {
            get;
            set;
        }
    void ShowWindow();
}

然后在所有窗口中实现它,无论它是什么类型。

 public partial class MainWindow : Window , IWindow
    {
        public MainWindow()
        {
            InitializeComponent();

        }



        public new int Left
        {
            get
            {
                return Left;
            }
            set
            {
                Left = value;
            }
        }

        public int Right
        {
            get
            {
                return Right;
            }
            set
            {
                Left = value;
            }
        }

        public void ShowWindow()
        {
            Show();
        }
    }

然后将参数更改为IWindow,如下所示:

private void SetMessagePosition(IWindow window, Controls.Button btn, string text))
        {

答案 1 :(得分:2)

您需要将其设为通用方法,以便传递类型:

private void SetMessagePosition<T>(Controls.Button btn, string text) where T : Window, new()
{
    T window = new T();
    relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));
    window.Left = relativePoint.X + this.Left;
    window.Top = relativePoint.Y + this.Top;
    window.Show();
}

然后,您可以这样称呼它:

SetMessagePosition<messageWindow>(btn, text);

但是,该方法的缺点是,当您通过泛型类型实例化对象时,无法将任何内容传递给构造函数。因此,您需要以不同的方式在窗口上设置文本,例如创建具有SetText方法的接口,或者类似的东西,并且可以将其添加为泛型类型的另一个约束。

你可以通过简单地期望新窗口已经被实例化并传递给方法而不是让方法自己实例化来简化整个混乱:

private void SetMessagePosition(Window window, Controls.Button btn)
{
    relativePoint = btn.TransformToAncestor(this).Transform(new Point(0, 0));
    window.Left = relativePoint.X + this.Left;
    window.Top = relativePoint.Y + this.Top;
    window.Show();
}

然后,您可以这样称呼它:

SetMessagePosition(new newWin(text), btn);
相关问题