I'm a former PHP developer now doing WPF/C# applications, and am having trouble understanding the best way to access global variables and how UserControls access the main Window, etc.
In the constructor of my Window1 class I load UserControls into an internal Dictionary, then dynamically load these into a DockPanel at runtime, which works great and keeps the internal state of each one.
public Window1()
{
InitializeComponent();
List<string> userControlKeys = new List<string>();
userControlKeys.Add("Welcome");
userControlKeys.Add("News");
Type type = this.GetType();
Assembly assembly = type.Assembly;
foreach (string userControlKey in userControlKeys)
{
string userControlFullName = String.Format("{0}.Pages.{1}", type.Namespace, userControlKey);
UserControl userControl = (UserControl)assembly.CreateInstance(userControlFullName);
_userControls.Add(userControlKey, userControl);
}
//set the default page
btnWelcome.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
Question: from within a UserControl how do I access my Dictionary in Window1?
- I can access the static Class Window1 but not its instantiation hence making a Getter for it gets me no further.
- there are global attributes in C# but I can imagine there is a better, more OOP way
- it just seems like I am looking at this in terms of web site / session variables, and thus missing a key concept of desktop development where "state is everywhere"
Hope someone can shed some light on this.