[英]How to access resource file's value in code behind
In the xml
I am able to access resource file's value by providing the corresponding key.在
xml
我可以通过提供相应的键来访问资源文件的值。 First I have added the namespace as following首先,我添加了命名空间如下
xmlns:res="clr-namespace:WpfApplication3.Properties"
And then bind the Text using following way然后使用以下方式绑定文本
Text="{x:Static res:Resources.city}"
But in my same application I have to use it in code behind.但是在我的同一个应用程序中,我必须在后面的代码中使用它。 What should I do it to access my resource file?
我应该怎么做才能访问我的资源文件?
When I count the key of my resource file I found it 0
that does not make any sense as I have many keys in my resource file.当我计算我的资源文件的键时,我发现它
0
没有任何意义,因为我的资源文件中有很多键。
这应该很简单:
var cityName = Properties.Resources.city;
If the resource file is local to the UserControl
/ Window
then try this ...如果资源文件位于
UserControl
/ Window
本地,请尝试此操作...
in the code behind at in the usings at the top ...在后面的代码中,在顶部的使用中......
using Resx = NameSpace.Resources
... call it what you like here. using Resx = NameSpace.Resources
... 在这里称其为您喜欢的using Resx = NameSpace.Resources
。
Wut NameSpace
?什么名字
NameSpace
? - Look for it in the .Designer.cs
resource file, the namespace that code is in. - 在
.Designer.cs
资源文件中查找,代码所在的命名空间。
then in the code behind ...然后在后面的代码中......
string actualText = Resx.Class.propertyNameOfResource
Wut Class
?吴
Class
? - this one you're in right now (pun intended) Wut propertyNameOfResource
- that's the Resource Key. - 你现在所处的这个(双关语)Wut
propertyNameOfResource
- 那是资源密钥。 VS Intellisense should work there. VS Intellisense 应该可以在那里工作。
Also handy is https://github.com/mrlacey/StringResourceVisualizer which shows you the actual strings in Visual Studio同样方便的是https://github.com/mrlacey/StringResourceVisualizer ,它显示了 Visual Studio 中的实际字符串
But you just broke any bindings you previously had if you assign it to a UI Control's content.但是,如果您将其分配给 UI 控件的内容,那么您只是破坏了以前拥有的任何绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.