Sag*_*gar 2 .net c# wpf user-interface static-classes
请告诉我以下哪项是一项优秀的编程实践:
1.使用静态类,然后使用类MainWindow构造函数对它的引用,如下所示:
public partial class Mainwindow : Window
{
public MainWindow()
{
InitializeComponent();
UI.window = this;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Shutdownads attempt1 = new Shutdownads();
}
}
static class UI
{
public static MainWindow window;
}
/*and then refering to the wpf elements from other classes as follows
UI.window.textbox.Text="blahblah"
UI.window.button ... and so on
*/
Run Code Online (Sandbox Code Playgroud)
要么
2.在MainWindow类中包含我的程序中的所有类是否更好?
要么
3.有没有更好的选择(也可以实现更好的OOP以及我可以通过其他类访问UI)?
从多个类控制UI元素通常是不好的做法.
您应该创建一个接口,该接口公开从其他类抽象UI的方法和属性,并在MainWindow类中实现该接口.
其他类可以接受该接口作为构造函数参数或从静态类中使用它.