WPF Application using a global variable

前端 未结 6 2030
余生分开走
余生分开走 2020-12-08 11:10

I created a WPF application in c# with 3 different windows, Home.xaml, Name.xaml, Config.xaml. I want to declare a variable in Home.xaml.cs that I

6条回答
  •  天命终不由人
    2020-12-08 11:51

    To avoid having to pass around values between windows and usercontrols, or creating a static class to duplicate existing functionality within WPF, you could use:

    • setting: App.Current.Properties["NameOfProperty"] = 5;
    • getting: string myProperty = App.Current.Properties["NameOfProperty"];

    This was mentioned above, but the syntax was a little off.

    This provides global variables within your application, accessible from any code running within it.

提交回复
热议问题