Universal App Resource在不同视图中重新生成值

时间:2016-01-10 17:44:10

标签: localization uwp

我尝试创建一个通用Windows应用程序,其中包含一个包含不同视图的主窗口。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
    <select name="numberOfAdults" id="numberOfAdults" tabindex="2">
        <option>-Adults-</option>
        <option value="1">One</option>
        <option value="2">Two</option>
    </select>
</div>

其中ContentFrame.Navigate(typeof(SimplePage)); 是XAML框架,ContentFrame是视图。

该项目有两个本地化。因此,我在解决方案中创建了一个文件夹SimplePage,其中包含两个文件夹Stringsen,其中包含每个de文件。

我想在SimplePage视图中使用Resources.resw文件中的字符串。所以我试过了:

resw

我也尝试使用tbSimpleInput1.Text = ResourceManager.Current.MainResourceMap.GetValue("Resources/dataToolDiameter", ResourceContext.GetForCurrentView()).ValueAsString; 代替ResourceContext.GetForViewIndependentUse(),但在尝试调试时我总是得到ResourceContext.GetForCurrentView()

在不同视图中访问资源的正确方法是什么?

这是Visual Studio中解决方案的屏幕截图:

Solution

1 个答案:

答案 0 :(得分:1)

如果您有单项目解决方案,我建议您创建一个Shell - 正如Microsoft示例所建议的那样,或者使用App.xaml.cs类来实现本地化目的。

首先,在任一类的构造函数中,获取当前的ResourceLoader

// E.g use the static constructor of your App class
static App()
{
    _resourceLoader = new ResourceLoader();
}

现在获取资源(例如文本)非常简单:

public static string GetLocalizedString(string key)
{
    return _resourceLoader.GetString(key);
}

现在您可以从默认资源字典加载字符串:

tbSimpleInput1.Text = App.GetLocalizedString("dataToolDiameter");

请注意:只有在项目中使用默认模式进行本地化时,此功能才有效。如果您使用不同的资源文件,则必须使用ResourceLoader构造函数的重载。