2013-06-28 58 views
0

嗨,我需要製作多語言WPF辦公室加載項。我想使用資源文件。我叫我ThisAddIn_Startup功能我SetLanguageDictionary在wpf中加載資源字典在應用程序中添加

public static void SetLanguageDictionary() 
    { 
     try 
     { 
     //Get the assembly information 
     System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); 
     Uri uriCodeBase = new Uri(assemblyInfo.CodeBase); 
     string path = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString()); 

     ResourceDictionary dict = new ResourceDictionary(); 

     switch (Thread.CurrentThread.CurrentCulture.ToString()) 
     { 
      case "en-US": 
       dict.Source = new Uri(path + "\\Resources\\en-US.xaml", 
           UriKind.Absolute); 
       break; 
      case "fr-FR": 
       dict.Source = new Uri(path + "\\Resources\\fr-FR.xaml", 
           UriKind.Absolute); 
       break; 
      default: 
       dict.Source = new Uri(path + "\\Resources\\en-US.xaml", 
           UriKind.Absolute); 
       break; 
     } 

      Application.Current.Resources.MergedDictionaries.Add(dict); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
    } 

我的資源文件是有

<ResourceDictionary 
     xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:system="clr-namespace:System;assembly=mscorlib"> 

    <system:String x:Key="Ready">Ready</system:String> 
    <system:String x:Key="login">Login</system:String> 
    <!-- All StringResources Goes Here --> 
</ResourceDictionary> 

加載時我有錯誤: 系統無效操作異常:操作錯誤loadfrom resouceDictionary與URI:「文件: /// c:

感謝您的回覆

回答

0

我解決了我的問題,這只是因爲加載o f resourcesdictionary只能由xaml頁面後面的代碼完成。

相關問題