嵌套屬性我有一個Singleton
類XAML綁定到的單
public sealed class GlobalTranslator
{
public Translator Translator = new Translator();
static readonly GlobalTranslator INSTANCE = new GlobalTranslator();
private GlobalTranslator()
{
}
public static GlobalTranslator Instance
{
get
{
return INSTANCE;
}
}
}
,讓我來定義文化爲我Translator
類
public class Translator : MarkupExtension
{
readonly CultureInfo Ci;
const string ResourceId = "LDVELH_WPF.Resources.Strings";
public Translator()
{
Ci = Thread.CurrentThread.CurrentCulture;
}
public Translator(String language)
{
switch (language.ToLower())
{
case "french":
Ci = new CultureInfo("fr-FR");
break;
case "english":
Ci = new CultureInfo("en-GB");
break;
default:
Ci = new CultureInfo("en-GB");
break;
}
}
public Translator(SupportedLanguage language)
{
switch (language)
{
case SupportedLanguage.French:
Ci = new CultureInfo("fr-FR");
break;
case SupportedLanguage.English:
Ci = new CultureInfo("en-GB");
break;
default:
Ci = new CultureInfo("en-GB");
break;
}
}
public string Text
{
get;
set;
}
override public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
ResourceManager Resmgr = new ResourceManager(ResourceId
, typeof(Translator).GetTypeInfo().Assembly);
return Resmgr.GetString(Text, Ci);
}
public string ProvideValue(string stringToTranslate)
{
Text = stringToTranslate;
if (Text == null)
return "";
ResourceManager Resmgr = new ResourceManager(ResourceId
, typeof(Translator).GetTypeInfo().Assembly);
return Resmgr.GetString(Text, Ci);
}
}
我以前的時候只用我的翻譯類,我可以這樣綁定:
Content="{Core:Translator Text=MyText}"
設置我的文本屬性w應該打電話給我的ProvideValue(IServiceProvider serviceProvider)
方法,所以我會綁定我MyText的resource.string。
既然我有多種語言,我正在嘗試使用我的GlobalTranslator
來做同樣的事情。 我可以做
<Window.Resources>
<x:StaticExtension Member="Core:GlobalTranslator.Instance" x:Key="MyGlobalTranslator"/>
</Window.Resources>
的引用添加到我的GlobalTranslator.Instance
,但我似乎無法找到一種方法做一些像
Content="{MyGlobalTranslator.Translator Text=MyText}"
我還是能做到這一點在後面的代碼由
MyButton.Content = GlobalTranslator.Instance.Translator.ProvideValue("MyText");
但想知道是否有辦法在xaml中做到這一點。