在我的解决方案中,我有以下项目类型:
视图
查看模型
在便携式项目中,我有一个.resx
文件,Access Modifier
设置为Public
(便携式项目仅支持.resx
个文件)
xxx.Commom\Resources\ViewResources.resx
然后要访问资源我有一个名为ResourceAccess.cs
的包装器,我必须创建一个包装器,因为.resx
生成一个internal
构造函数。
namespace xxx.Common.Resources
{
public class ResourceAccessor
{
private static ViewResources _appResources;
public static ViewResources AppResources
{
get
{
if(_appResources== null)
{
_appResources= new ViewResources();
}
return _appResources;
}
}
}
}
在Windows Phone 8.0和Windows 8.1项目的视图中,我有一个名为LocalizedStrings.cs
using xxx.Common.Resources;
namespace xxx
{
public class LocalizedStrings
{
public ViewResources LocalizedResources { get { return ResourceAccessor.AppResources; } }
}
}
Windows Phone 8.0项目
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:xxx" x:Key="LocalizedStrings"/>
</Application.Resources>
Windows 8.1共享项目
<Application
xmlns:local="using:xxx">
<Application.Resources>
<local:LocalizedStrings x:Key="LocalizedStrings"/>
</Application.Resources>
</Application>
在Windows Phone 8.0项目中,这有效:
<TextBlock Text="{Binding Path=LocalizedResources.CommonResxText, Source={StaticResource LocalizedStrings}}"/>
但是在Windows 8.1项目中,上述操作无效。
如何引用Windows 8.1(WinRT)中.resx
文件中的字符串?
我查看了MSDN中的Using string resources,但这对我没有帮助,因为我无法将resx
更改为便携式项目中的resw
(实际上只是引用.Net
)。
答案 0 :(得分:2)
长时间盯着this问题
我看到我的Windows 8.1项目中的绑定错误,这是正确的绑定
<TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.CommonResxText}"/>
此方法适用于Debug
当项目在Release
时,它不会。
在自动生成的文件ViewResources.Designer.cs
System.Resources.ResourceManager.GetString("CommonResxText", resourceCulture);
抛出异常,因为程序集中不存在字符串;它们没有编译成Release
详细了解此问题here
答案 1 :(得分:-2)
为什么需要所有包装类?为什么不直接引用资源文件内容?
为此,您只需使资源文件生成代码(在.resx屏幕的组合框中使用Access Modifier:Public),然后将文本直接绑定到视图,如下所示:
<TextBlock Text="{x:Static resx:ViewResources.MyText}" />
只需确保在您的Window / UserControl中将resx添加为xml命名空间:
<Window xmlns:resx="clr-namespace:xxx.Common.Resources" ... />