首先添加一个主资源文件Text.resx 设置生成代码 public,然后添加Text.zh-cn.resx,Text.en-us.resx等等资源文件设置不生成代码。
程序会自动生成ResourceManager类,
添加代码:
System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-us"); System.Threading.Thread.CurrentThread.CurrentCulture = c;// new System.Globalization.CultureInfo("zh-CN"); System.Threading.Thread.CurrentThread.CurrentUICulture = c;// new System.Globalization.CultureInfo("zh-CN");
设置本地化
获取资源文件中的key value
this.txtName.Text = SL_ResourceDemo.Resource.Text.name; this.txtSex.Text = SL_ResourceDemo.Resource.Text.sex;
注意在生成的xap 文件中不包含这些资源文件的dll文件。解决方法:
找到silverlight工程文件打开会看到
<ProjectGuid>{DC4A1B28-8511-491D-B209-D2A31A8501E7}</ProjectGuid> <ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>Shareidea.Web.UI.Control.Workflow.Designer</RootNamespace> <AssemblyName>ShareDesigner</AssemblyName> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <SilverlightApplication>true</SilverlightApplication> <SupportedCultures> en-us,zh-cn </SupportedCultures> <XapOutputs>true</XapOutputs> <GenerateSilverlightManifest>true</GenerateSilverlightManifest> <XapFilename>ShareDesigner.xap</XapFilename> <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate> <SilverlightAppEntry>design.App</SilverlightAppEntry> <TestPageFileName>TestPage.html</TestPageFileName> <CreateTestPage>true</CreateTestPage> <ValidateXaml>true</ValidateXaml> <ThrowErrorsInValidation>false</ThrowErrorsInValidation> <SccProjectName>SAK</SccProjectName> <SccLocalPath>SAK</SccLocalPath> <SccAuxPath>SAK</SccAuxPath> <SccProvider>SAK</SccProvider> <FileUpgradeFlags>
注意这里:
<SupportedCultures> en-us,zh-cn </SupportedCultures>
这样生成xap文件就会包括 这两个语言dll文件了。
可以将本地化设置放到独立存储中,下次打开的时候直接读取独立存储中的数据
public class Configure { static System.Globalization.CultureInfo currentCulture; public static System.Globalization.CultureInfo CurrentCulture { get { if (currentCulture == null) { try { System.IO.IsolatedStorage.IsolatedStorageSettings appSetting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings; if (appSetting.Contains("language")) { currentCulture = new System.Globalization.CultureInfo((string)appSetting["language"]); } } catch (Exception e) { } } if (currentCulture == null) { currentCulture = new System.Globalization.CultureInfo("en-us"); } return currentCulture; } set { currentCulture = value; System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = currentCulture; try { System.IO.IsolatedStorage.IsolatedStorageSettings appSetting = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings; if (appSetting.Contains("language")) { appSetting["language"] = currentCulture.Name; } else { appSetting.Add("language", currentCulture.Name); } } catch (Exception e) { } } } }