Question:
Actually, I would like to refer to the UI elements of the main application window, but it’s not clear how. It is clear that it is easy to access controls from the window class itself, but it is not clear how to access them from a third-party one. (Passing them by reference is not good.)
Passing controls or the window itself to the class by reference is not good, since it has methods that must be asynchronously executed with window handlers. Is there any other way? Otherwise, you will have to use ISynchronizeInvoke, which I would like to avoid.
Answer:
Of course, you should not pass controls by reference. You need to create an event in the main window and notify it through this event about the need to change controls. This is if the main window and the one who sends it a notification are in the same thread.
// Какой то класс
public class MyClass
{
public event MyDelegate MyDelegateEvent; // Ваш евент
public void f()
{
// При вызове функции f() уведомляем главное окно (и всех кто подпишется на него)
MyDelegateEvent();
}
}
public delegate void MyDelegate(); // Делегат
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyClass my = new MyClass(); // Это просто создание нашего класса
// Создаем наше уведомление
my.MyDelegateEvent += new MyDelegate(my_MyDelegateEvent);
}
void my_MyDelegateEvent()
{
// Изменяем нужные контролы
}
}