【问题标题】:String resource from .resw for Data Binding in Windows 8来自 .resw 的字符串资源,用于 Windows 8 中的数据绑定
【发布时间】:2023-03-26 06:41:01
【问题描述】:

来自.resw 的字符串资源,用于 Windows 8 中的数据绑定。

在 Windows Phone 中我使用如下:

  1. 创建AppStrings.resx 并将所有字符串放在上面。
  2. 创建类StringResources,其字段返回AppStrings.resx 的实例。
  3. StringResources 添加到ApplicationResource

StringResources.cs

 public class StringResources
    {
        private static AppStrings _resources;

        public static AppStrings LocalizedResources
        {
            get { return _resources ?? (_resources = new AppStrings()); }
        }
    }

在 App.xaml 中

<Application.Resources>
        <ResourceDictionary>
            <res:StringResources x:Key="Strings"/>
        </ResourceDictionary>
    </Application.Resources>

使用我来自 xaml 的资源。

 Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource Strings}}"

一切都很好,但我不能在 Windows 8 中做到这一点。
我正在搜索与 Windows 8 上的字符串资源类似的使用方式 DataBinding

注意:
我检查了MSDN Sample,但找不到我需要的。
我也检查了ResW File Code Generator,这是可行的方法,但太牵强了.

【问题讨论】:

    标签: xaml data-binding windows-runtime


    【解决方案1】:

    您可以使用与 WP 相同的方法。 Visual Studio 扩展 PublicResXFileCodeGenerator 用于生成强类型类,其静态属性对应于 WinPhone 应用程序中 resx 文件中的所有键。但是由于某种原因,默认情况下 W8 没有这样的工具。

    ResW 文件代码生成器 Visual Studio 扩展正在做同样的事情。

    http://visualstudiogallery.msdn.microsoft.com/3ab88efd-1afb-4ff5-9faf-8825a282596a

    安装它,然后您只需将“自定义工具”字段设置为 ReswFileCodeGenerator(在您的默认 AppStrings.resw 文件的属性中)并设置自定义工具命名空间为您的 AppStrings 类提供命名空间。 它会自动生成 AppStrings 类,然后你可以像在 WP 中一样使用它。

    注意:每次更改 AppString.resw 文件时,此扩展都会重新生成 AppStrings 类,VS 2013 会将构造函数“new ResourceLoader("AppStrings")”标记为已弃用。您需要改用 CoreWindow 独立方法 ResourceLoader.GetForViewIndependentUse("AppString") (GetForCurrentView("AppString") 不起作用)。

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多