1

我想知道设置的正确方法(最好看),例如,来自除 my 之外的类的标签MainWindow

目前,我会做这样的事情:

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

        MyClass a=new MyClass(this);
        a.WriteToLabel();
    }
}

class MyClass {
    MainWindow parent;

    public MyClass(MainWindow parent) {
        this.parent=parent;
    }

    public void WriteToLabel() {
        parent.label1.Text="Test";
    }
}

但我觉得这是一种不好的做法。
当你有超过 20 个类并且都有一个parent.
你会如何解决这样的问题?

4

5 回答 5

3

为什么不label1作为参数传递?

和:

class MyClass
{
    public void WriteToLabel(Label label)
    {
        label.Content = "Test";
    }
}

你会使用:

var a = new MyClass();
a.WriteToLabel(label1);

按照惯例,最好尽可能避免代码中的耦合。我不会添加对窗口的引用。我宁愿尽可能地参数化我的方法。

于 2013-01-12T21:52:36.853 回答
2

I think your practice is overall good. But I would also "keep track" of the children I've instanciated through a List so I could also, eventually, search for a specific one using Linq extensions. I also corrected some minor errors in your code (naming convention for the parent member of MyClass and the void result of WriteToLabel() being assigned to MyClass instance in the parent constructor.

public partial class MainWindow : Window
{
    private List<MyClass> children;

    public MainWindow()
    {
        children = new List<MyClass>();

        InitializeComponent();

        MyClass child = InstantiateChild();
        child.WriteToLabel();
    }

    private MyClass InstantiateChild()
    {
        MyClass child = new MyClass(this);
        children.Add(child);

        return child;
    }
}

public class MyClass
{
    private MainWindow m_Parent;

    public MyClass(MainWindow parent)
    {
        m_Parent = parent;
    }

    public void WriteToLabel()
    {
        m_Parent.label1.Text = "Test";
    } 
}
于 2013-01-12T22:04:40.647 回答
0

使用流畅的界面

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass a = new MyClass(this).WriteToLabel("Test");
    }
}

class MyClass
{
    MainWindow parent;

    public MyClass(MainWindow parent)
    {
        this.parent = parent;
    }

    public MyClass WriteToLabel(string txt)
    {
        parent.label1.Text = txt;
        return this;
    } 
}
于 2013-01-12T21:54:34.827 回答
0

问题是为什么你需要从一个LabelText属于MainWindow. 标签的形式是它的 GUI 的控制器,类只需要提供你想在表单中显示的内容。

所以这是更好的做法。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass a = new MyClass();
        string data = a.GetData();
        label1.Text = text;
    }
}

class MyClass
{
    public MyClass()
    {
        this.parent = parent;
    }

    public stringGetData()
    {
        return "Test";
    } 
}
于 2013-01-12T21:54:45.077 回答
0

由于您的班级彼此完全了解,因此提供属性访问器是一个好方法。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass a = new MyClass(this);
        a.WriteToLabel();
    }


    public string LabelText
    {  
        get { return label1.Text; }
        set {
                if (this.InvokeRequired) 
                {
                   this.BeginInvoke(
                       delegate() => 
                                { 
                                  label1.Text =value;
                                })};
                 }
                 else
                 {
                    label1.Text = value;
                 }
              }

}

class MyClass
{
    MainWindow parent;

    public MyClass(MainWindow parent)
    {
        this.parent = parent;
    }
    public void WriteToLabel()
    {
        parent.LabelText = "Test";
    } 
}
于 2013-01-12T21:58:50.950 回答