I have this problem. I create two instaces of my classes in main window. An I need create other slave window in code behind and in this window I need consume method of objects created in main window.
public partial class MainWindow
{
//internal MyInterface1 MyObject1;
//internal MyInterface2 MyObject2;
internal MyClass1 MyObject1;
internal MyClass2 MyObject2;
public MainWindow()
{
InitializeComponent();
//create instances
MyObject1 = new MyClass1();
MyObject2 = new MyClass2();
}
private void SomeMethod()
{
//in this window I neew consume some method and propertie of MyObject1 and MyObject1
//my temporary solution is:
var slaveWindow = new SlaveWindow(MyObject1, MyObject2);
}
}
Can exist suitable solution, thank you.
There’s nothing wrong with your “temporary” solution. I’d do it the same way.
Alternatively, you could publicly expose the two objects and then pass the
MainWindowto theSlaveWindow:Whichever solution is preferable probably depends on the nature of your two objects. But I think it’s fairly safe to say that your current solution is just fine.