C# 使用全局变量的 WPF 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1161459/
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
WPF Application using a global variable
提问by Jake
I created a WPF application in c# with 3 different windows, Home.xaml, Name.xaml, Config.xam
l. I want to declare a variable in Home.xaml.cs
that I can use in both the other forms. I tried doing public string wt = "";
but that didn't work.
我在 c# 中创建了一个 WPF 应用程序,其中包含 3 个不同的窗口,Home.xaml, Name.xaml, Config.xam
l。我想声明一个变量Home.xaml.cs
,我可以在其他两种形式中使用它。我尝试这样做,public string wt = "";
但没有奏效。
How can I make it usable by all three forms?
我怎样才能使它对所有三种形式都可用?
回答by Eduardo Cobuci
You can use a static property:
您可以使用静态属性:
public static class ConfigClass()
{
public static int MyProperty { get; set; }
}
Edit:
编辑:
The idea here is create a class that you holds all "common data", typically configurations. Of course, you can use any class but suggest you to use a static class. You can access this property like this:
这里的想法是创建一个类来保存所有“公共数据”,通常是配置。当然,您可以使用任何类,但建议您使用静态类。您可以像这样访问此属性:
Console.Write(ConfigClass.MyProperty)
回答by Ari Roth
There are two different things you can do here (among others; these are just the two that come to mind first).
您可以在这里做两种不同的事情(除其他外;这只是首先想到的两种)。
You could make the variable static on Home.xaml.cs
public static string Foo = "";
You could just pass in the variable to all three forms.
您可以在 Home.xaml.cs 上将变量设为静态
公共静态字符串 Foo = "";
您可以将变量传递给所有三种形式。
I would go with #2, myself, and if necessary create a separate class that contains the data I need. Then each class would have access to the data.
我会选择#2,我自己,如果有必要,创建一个单独的类来包含我需要的数据。然后每个类都可以访问数据。
回答by Henk Holterman
The proper way, especially if you ever want to move to XBAPP, is to store it in
正确的方法,特别是如果你想移动到 XBAPP,是将它存储在
Application.Current.Properties
which is a Dictionary object.
这是一个 Dictionary 对象。
回答by Dustin Pauze
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:
为了避免在窗口和用户控件之间传递值,或创建静态类来复制 WPF 中的现有功能,您可以使用:
- setting:
App.Current.Properties["NameOfProperty"] = 5;
- getting:
string myProperty = App.Current.Properties["NameOfProperty"];
- 环境:
App.Current.Properties["NameOfProperty"] = 5;
- 得到:
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.
这在您的应用程序中提供了全局变量,可从其中运行的任何代码访问。
回答by duyanhphamkiller
App.xaml:
应用程序.xaml:
<Application x:Class="WpfTutorialSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="WPF application/ResourcesFromCodeBehindSample.xaml">
<Application.Resources>
<sys:String x:Key="strApp">Hello, Application world!</sys:String>
</Application.Resources>
code behind
背后的代码
Application.Current.FindResource("strApp").ToString()
回答by Mark Adamantine
As other people mentioned before either use App.Current.Properties
or create a static class.
I am here to provide an example for those who need more guidance with the static class.
正如之前提到的其他人使用App.Current.Properties
或创建静态类。我在这里为那些需要更多静态类指导的人提供一个示例。
- Add a new Class
- 添加新班级
Right-click your project name in your solution explorer
Add > New Item
chooseClass
give it a name (I usually name it GLOBALS)
在解决方案资源管理器中右键单击您的项目名称,
Add > New Item
选择Class
给它一个名称(我通常将其命名为 GLOBALS)
- What that cs file should look like
- 那个 cs 文件应该是什么样子的
using System;
namespace ProjectName
{
public static class GLOBALS
{
public static string Variable1 { get; set; }
public static int Variable2 { get; set; }
public static MyObject Variable3 { get; set; }
}
}
- Add references into .cs files where you intend to use those variables
- 将引用添加到您打算使用这些变量的 .cs 文件中
using ProjectName
using ProjectName
- We're done. Use it. Examples:
- 我们完成了。用它。例子:
GLOBALS.Variable1 = "MyName"
Console.Write(GLOBALS.Variable1)
GLOBALS.Variable2 = 100;
GLOBALS.Variable2 += 20;
GLOBALS.Variable3 = new MyObject();
GLOBALS.Variable3.MyFunction();
On a side note, do notice that using the static class as global variables in c# is considered a bad practice (that's why there is no official implementation for globals), but I consider it a shortcut for when I am too lazy haha. It should not be used in the professional environment.
附带说明一下,请注意在 c# 中使用静态类作为全局变量被认为是一种不好的做法(这就是为什么没有全局变量的官方实现),但我认为这是我太懒时的捷径哈哈。它不应该在专业环境中使用。