2

I have created an instance of a window inside a class, but I am unable to access the instance of the window from my other class directly.

Is there a way to reference the window instance I have already created using a C# method, perhaps searching through the open app windows until it finds the Dashboard window I am trying to access?

wonea
  • 4,783
  • 17
  • 86
  • 139
Joel Kennedy
  • 1,581
  • 5
  • 19
  • 41
  • 2
    Sounds like a poor design in general. There are likely better ways to accomplish what you are trying to do. How about telling us what you need from that window in the first place and why other classes need it too? What you are doing is a sure way to write fragile, tightly-coupled code. – Ed S. May 26 '11 at 21:29
  • I'd recommend using a singleton pattern for the main window – Manticore Feb 22 '20 at 01:50

2 Answers2

6

Application.Current.Windows gives you all windows, shouldn't be hard to find using its type.

(As Ed pointed out this does not sound like very good design, so you might want to think about how you can get things done without messy window references)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Where could I construct the window in a better place so that all methods could access the instance? Currently I create the instance inside a class, but things are already getting messy like you said. I wouldn't want to construct the window in the MainWindow as it should only be created once it's needed - that's the only problem. – Joel Kennedy May 26 '11 at 21:52
  • You don't need to create it, just declare a reference and use it once you instatiate the window. Should of course check for null in any calls to that reference as it may not contain a window yet. – H.B. May 26 '11 at 22:00
  • I tried to do it like you said, but the reference doesn't contain my Dashboard window when I try to use it from other class because it doesn't exist yet. I don't know how I can get around this problem, and make the class understand that the window will exist. – Joel Kennedy May 26 '11 at 23:38
  • You are not supposed to hoax the class into thinking that the window will exist, because it won't necessarily. As i said, in every case where you get the reference you first have to check if it points to null or not. If this is a problem because the window should exist then why not just create it? Anyway, if all this still is a problem you probably have a bad design. – H.B. May 26 '11 at 23:48
  • I managed to create the window reference in a public location and class B accepted that the instance was created in class A this time. Thanks for the help. – Joel Kennedy May 28 '11 at 11:18
5
System.Reflection.Assembly assemby = System.Reflection.Assembly.GetExecutingAssembly();
System.Type[] types = assemby.GetTypes();
var varWindows = types.ToList()
    .Where(current => current.BaseType == typeof(Window));
MessageBox.Show(varWindows.Count().ToString());

Application.Current.Windows gets us all instantiated windows, but the above code get us all windows.

Rüdiger Hanke
  • 6,215
  • 2
  • 38
  • 45
Hamed Khatami
  • 525
  • 1
  • 5
  • 14