ResourceDictionary中源绑定到模块(本地化) [英] ResourceDictionary Source Binding to Module (for Localization)

查看:895
本文介绍了ResourceDictionary中源绑定到模块(本地化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一组绑定的对象,像这样的字符串的XAML窗口:

I have a XAML Window that has a set of strings that are bound to objects like so:

<Label Content="{StaticResource LabelUserName}" HorizontalAlignment="Right" Name="Label1" VerticalAlignment="Center" />

这code正常工作时,我这样定义我ResouceDictionary:

This code works fine when I define my ResouceDictionary like this:

<Window.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Strings/Strings_en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <ObjectDataProvider x:Key="objLogon" ObjectType="{x:Type StarUtilities:Logon}" />
    </ResourceDictionary>
</Window.Resources>

不过,我想比布尔改变那里的字符串从绑定的,所以我创建了一个模块,看起来像这样:

However, I want to beable to change where the strings are bound from, so I created a module that looks like this:

Public Module LocalizationChooser
    Public ReadOnly Property GetLocalization As Uri
        Get
            Return New Uri("/Strings/Strings_en-US.xaml", UriKind.Relative)
        End Get
    End Property
End Module

然后我决定我会尝试改变来源来一个绑定字符串。不过,我已经试过以下,没有工作:

Then I decided I would try changing the Source to a Binding string. However, I've tried the following and none work:

<ResourceDictionary Source="{Binding Source={x:Static Member=StarUI:LocalizationChooser.GetLocalization}}" />
<ResourceDictionary Source="{x:Static Member=StarUI:LocalizationChooser.GetLocalization}" />
<ResourceDictionary Source="{Binding Source=StarUI:LocalizationChooser.GetLocalization}" />
<ResourceDictionary Source="{Binding Source=LocalizationChooser, Path=GetLocalization}" />
<ResourceDictionary Source="{Binding Source=StarUI:LocalizationChooser, Path=GetLocalization}" />
<ResourceDictionary Source="{Binding Source={x:Static Member=StarUI:LocalizationChooser.GetLocalization}, Path=GetLocalization}" />
<ResourceDictionary Source="{Binding Source={x:Static Member=StarUI:LocalizationChooser.GetLocalization}, Path=LocalizationChooser.GetLocalization}" />
<ResourceDictionary Source="{Binding Source={x:Static Member=StarUI:LocalizationChooser.GetLocalization}, Path=StarUI:LocalizationChooser.GetLocalization}" />

所有生成相同的错误:

All generate the same error:

ArgumentNullException被抛出的的ResourceDictionary:值不能为空结果
  参数名称:项目

ArgumentNullException was thrown on "ResourceDictionary": Value cannot be null.
Parameter name: item

我能做些什么来得到这个工作?

What can I do to get this working?

我要指出的StarUI命名空间已被定义如下:

I should mention that the StarUI namespace is already defined as the following:

的xmlns:StarUI =CLR的命名空间:StarUI

编辑:的结果
我发现,与变化我到模块,我可以得到code编译和运行,如果我使用做:


I found that with the change I made to the module I can get the code to compile and run if I use:

<ResourceDictionary Source="{x:Static StarUI:LocalizationChooser.GetLocalization}" />

不过,我仍然没有设计时支持。由于模块返回,这并不需要查找任何一个静态项目,是有办法在这里得到的设计时支持?如果没有,是否有在XAML指定设计时的运行时的静态引用,然后他们动态参考的方式?

However, I still don't have design-time support. Since the module is returning a static item that doesn't need to lookup anything, is there a way to get design-time support here? If not, is there a way to specify in the XAML a static reference for design-time and then they dynamic reference for run-time?

编辑2 :结果
我终于做到了是写在XAML指定来源静态,然后设置构造函数来覆盖了。因为具有相同名称的词典条目将基于最后一个附加引用,我还可以添加在运行时的字典,但仍具有在设计时的字典。

EDIT 2:
What I finally did was to write the XAML specifying the Source statically and then setting the constructor to 'overwrite' that. Since dictionary entries with the same name will be referenced based on the last one added, I can add a dictionary at run-time but still have a dictionary at design-time.

我用下面简单地定位我的code,包括设计时支持和扶持的语言动态变化。

I used the following to localize my code simply, including design-time support and enabling dynamic changing of languages.

在我的XAML我包括:

In my XAML I included:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Name="LocalizationStrings" Source="/Strings/Strings_en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <ObjectDataProvider x:Key="objLogon" ObjectType="{x:Type StarUtilities:Logon}" />
    </ResourceDictionary>
</Window.Resources>

在我的code-背后:

In my code-behind:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.Resources.MergedDictionaries.Add(GetLocalization)
End Sub

的getLocation是一个公共模块:

GetLocation is in a Public Module:

Public ReadOnly Property GetLocalization As ResourceDictionary
    Get
        Dim resourceLocalization As New ResourceDictionary
        resourceLocalization.Source = New Uri("/Strings/Strings_zh-cn.xaml", UriKind.Relative)
        Return resourceLocalization
    End Get
End Property

我的ResourceDictionary XAML的样本是:

A sample of my ResourceDictionary XAML is:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="ButtonLogon">Logon</sys:String>
    <sys:String x:Key="ButtonCancel">Cancel</sys:String>
    <sys:String x:Key="LabelUserName">User Name:</sys:String>
    <sys:String x:Key="LabelPassword">Password:</sys:String>
    <sys:String x:Key="LabelKey">Decryption Key:</sys:String>
</ResourceDictionary>

然后我通过访问语言字符串:

Then I just access the language string via:

<Label Content="{DynamicResource LabelUserName}" Name="Label1" />

现在一切都很正常!

推荐答案

您需要从静态资源切换到的 DynamicResource ,因为你的资源不是立即可用的。

You would need to switch from StaticResource to DynamicResource, since your resource isn't immediately available.

但是你不能使用上的ResourceDictionary属性绑定,因为它不自DependencyObject派生

But you cannot using Bindings on the ResourceDictionary properties, as it does not derive from DependencyObject.

这篇关于ResourceDictionary中源绑定到模块(本地化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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