它是使用静态类来使得UI元素从所有类的.NET访问一个好的做法呢? [英] Is it a good practice to use static classes to making the UI elements accessible from all the classes in .NET?

查看:88
本文介绍了它是使用静态类来使得UI元素从所有类的.NET访问一个好的做法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我知道以下哪些是一个良好的编程习惯:

Please let me know which of the following is a good programming practise:

1。使用静态类,然后使用从类的引用,其​​主窗口的构造函数,如下所示:

    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
    */

2。它是更好地包括在我的主窗口类节目的所有类?

3。有没有更好的选择(即还可以通过其他类实现更好的面向对象的,以及我能acccess UI)?

推荐答案

这是通常较差的做法来控制从多个类的UI元素。

It is usually poor practice to control UI elements from multiple classes.

您应该创建一个公开的方法和属性抽象的其他类用户界面,并实现了主窗口类接口的接口。

You should create an interface that exposes methods and properties that abstract the UI from the other classes, and implement that interface in the MainWindow class.

其他类可以接受的接口作为构造函数的参数或从静态类使用它。

The other classes can accept that interface as a constructor parameter or use it from a static class.

这篇关于它是使用静态类来使得UI元素从所有类的.NET访问一个好的做法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆