繁体   English   中英

如何在Windows 8.1(WinRT)应用程序中引用.resx文件?

[英]How to reference a .resx file in a Windows 8.1 (WinRT) app?

在我的解决方案中,我具有以下项目类型:

查看

  • Windows Phone 8.1(通用)
  • Windows 8.1(通用)
  • Windows Phone 8.0

查看模型

  • 可移植项目(命名空间xxx.Common)

查看模型

在可移植项目中,我有一个.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; } }
    }
}

App.xaml文件

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>

Main.xaml

在Windows Phone 8.0项目中,该方法有效:

<TextBlock Text="{Binding Path=LocalizedResources.CommonResxText, Source={StaticResource LocalizedStrings}}"/>

但是在Windows 8.1项目中,以上操作无效。

在Windows 8.1(WinRT)中,如何在.resx文件中引用字符串?

我看过使用 MSDN中的字符串资源 ,但这无济于事,因为我无法在可移植项目(实际上仅引用.Net )中将resx更改为resw

长时间盯着这个问题

我看到Windows 8.1项目中的绑定错误,这是正确的绑定

<TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.CommonResxText}"/>

PS

这种方法有效...在Debug

当项目在Release它不在。

在自动生成的文件ViewResources.Designer.cs

System.Resources.ResourceManager.GetString("CommonResxText", resourceCulture);

抛出异常,因为字符串在程序集中不存在; 它们没有被编译为Release的二进制文件

在此处阅读有关此问题的更多信息

为什么需要所有包装器类? 为什么不直接引用资源文件的内容?

为此,您只需使资源文件生成代码(在.resx屏幕的组合框中使用Access Modifier:Public),然后将文本直接绑定到视图,如下所示:

<TextBlock Text="{x:Static resx:ViewResources.MyText}" />

只要确保在您的Window / UserControl中将resx添加为xml命名空间即可:

<Window xmlns:resx="clr-namespace:xxx.Common.Resources" ... />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM