Please let me know which of the following is a good programming practise:
1. Using a static class and then using a reference to it from the class MainWindow constructor as shown:
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
*/
or
2. Is it better to include all the classes in my program in the MainWindow class?
or
3. Is there a better option (that also implements better OOP as well as I can acccess UI through other classes)?
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.