wpf - ResourceDictionary Source Binding to Module (for Localization) -
i have xaml window has set of strings bound objects so:
<label content="{staticresource labelusername}" horizontalalignment="right" name="label1" verticalalignment="center" /> this code works fine when define resoucedictionary 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, want beable change strings bound from, created module looks this:
public module localizationchooser public readonly property getlocalization uri return new uri("/strings/strings_en-us.xaml", urikind.relative) end end property end module then decided try changing source binding string. however, i've tried following , 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 same error:
argumentnullexception thrown on "resourcedictionary": value cannot null.
parameter name: item
what can working?
i should mention starui namespace defined following:
xmlns:starui="clr-namespace:starui"
edit:
found change made module can code compile , run if use:
<resourcedictionary source="{x:static starui:localizationchooser.getlocalization}" /> however, still don't have design-time support. since module returning static item doesn't need lookup anything, there way design-time support here? if not, there way specify in xaml static reference design-time , dynamic reference run-time?
edit 2:
did write xaml specifying source statically , setting constructor 'overwrite' that. since dictionary entries same name referenced based on last 1 added, can add dictionary @ run-time still have dictionary @ design-time.
i used following localize code simply, including design-time support , enabling dynamic changing of languages.
in xaml 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> in code-behind:
public sub new() ' call required designer. initializecomponent() ' add initialization after initializecomponent() call. me.resources.mergeddictionaries.add(getlocalization) end sub getlocation in public module:
public readonly property getlocalization resourcedictionary dim resourcelocalization new resourcedictionary resourcelocalization.source = new uri("/strings/strings_zh-cn.xaml", urikind.relative) return resourcelocalization end end property a sample of 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 access language string via:
<label content="{dynamicresource labelusername}" name="label1" /> now works great!
you need switch staticresource dynamicresource, since resource isn't available.
but cannot using bindings on resourcedictionary properties, not derive dependencyobject.
Comments
Post a Comment