如何从代码隐藏的资源文件中检索字符串? [英] How to retrieve string from resource file in codebehind?

查看:38
本文介绍了如何从代码隐藏的资源文件中检索字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主项目中有一个资源字典.

I have a Resource Dectionary in main project.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib">

<system:String x:Key="ToolbarNewButton" >Add</system:String>
<system:String x:Key="localizedMessage" >Delete Item</system:String>

和将其添加到主项目的类库.我在其中使用了 DynamicResource.

and a class library that add it to main project. I use DynamicResource in it.

 <Label Content="{DynamicResource ToolbarNewButton}"  Foreground="{DynamicResource InputFontColor}"/>

我想从代码隐藏的资源文件中检索字符串.我使用此代码但得到 ResourceReferenceKeyNotFoundException

I want to retrieve string from resource file in codebehind. I use this code but get ResourceReferenceKeyNotFoundException

string localizedMessage = (string)Application.Current.Resources["localizedMessage"];

推荐答案

我想这是第一个代码如下:

I suppose it's the first one Here's the code:

ResourceDictionary rd = System.Windows.Application.Current.Resources.MergedDictionaries[0];

ResourceDictionary rd = System.Windows.Application.Current.Resources.MergedDictionaries[0];

object theValue = rd["localizedMessage"];

object theValue = rd["localizedMessage"];

string theString = (string)theValue;

string theString = (string)theValue;

这篇关于如何从代码隐藏的资源文件中检索字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆