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#中创建了一个带有3个不同窗口的WPF应用程序,Home.xaml,Name.xaml,Config.xaml。我想在Home.xaml.cs中声明一个我可以在其他两种形式中使用的变量。我试过做公共字符串wt =“”;但那没用。
How can I make it usable by all three forms?
我怎样才能使这三种形式都可用?
5 个解决方案
#1
21
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对象。
#2
11
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)
#3
11
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:
为了避免在Windows和用户控件之间传递值,或者创建静态类来复制WPF中的现有功能,您可以使用:
- setting:
App.Current.Properties["NameOfProperty"] = 5;
- 设置:App.Current.Properties [“NameOfProperty”] = 5;
- getting:
string myProperty = App.Current.Properties["NameOfProperty"];
- 获取: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.
这在您的应用程序中提供了全局变量,可以从其中运行的任何代码访问。
#4
1
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
您可以在Home.xaml.cs上将变量设置为static
public static string Foo = "";
public static string Foo =“”;
-
You could just pass in the variable to all three forms.
您可以将变量传递给所有三种形式。
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,如有必要,创建一个包含我需要的数据的单独类。然后每个类都可以访问数据。
#5
0
App.xaml:
App.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()
#1
21
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对象。
#2
11
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)
#3
11
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:
为了避免在Windows和用户控件之间传递值,或者创建静态类来复制WPF中的现有功能,您可以使用:
- setting:
App.Current.Properties["NameOfProperty"] = 5;
- 设置:App.Current.Properties [“NameOfProperty”] = 5;
- getting:
string myProperty = App.Current.Properties["NameOfProperty"];
- 获取: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.
这在您的应用程序中提供了全局变量,可以从其中运行的任何代码访问。
#4
1
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
您可以在Home.xaml.cs上将变量设置为static
public static string Foo = "";
public static string Foo =“”;
-
You could just pass in the variable to all three forms.
您可以将变量传递给所有三种形式。
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,如有必要,创建一个包含我需要的数据的单独类。然后每个类都可以访问数据。
#5
0
App.xaml:
App.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()