如何在 WPF 中创建全局列表变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22430666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a global list variable in WPF
提问by Adrien Neveu
I want, in my WPF app, to store all my log in a list variable.
我想在我的 WPF 应用程序中,将我的所有日志存储在一个列表变量中。
I have two pages, Home.xaml and Settings.xaml.
我有两个页面,Home.xaml 和 Settings.xaml。
How can I access the same variable in the two pages?
如何在两个页面中访问相同的变量?
回答by JaredPar
If you want a truly global variable then you can use a static
如果你想要一个真正的全局变量,那么你可以使用 static
static class Container {
public static List<string> LogList = new List<string>();
}
Any page can now access this value with Container.LogList
现在任何页面都可以访问此值 Container.LogList
回答by Balaji
The Application class already has this functionality built in.
Application 类已经内置了这个功能。
// Set an application-scope resource
Application.Current.Resources["ApplicationScopeResource"] = Brushes.White;
...
// Get an application-scope resource
Brush whiteBrush = (Brush)Application.Current.Resources["ApplicationScopeResource"
Also check these links for more info
另请查看这些链接以获取更多信息
Storing user details in application variable
How can I save global application variables in WPF?

