在wpf中加载资源字典添加到应用程序中

时间:2013-06-28 15:30:44

标签: resources outlook-addin

您好我需要制作多语言WPF办公室加载项。我想使用资源文件。我在我的ThisAddIn_Startup函数中调用了我的SetLanguageDictionary

 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>

加载时我有错误: 系统无效操作异常:操作错误从resouceDictionary加载URI:“file:/// c:

感谢您的回复

2 个答案:

答案 0 :(得分:0)

我解决了我的问题。这只是因为资源字典的加载只能通过xaml页面后面的代码来完成。

答案 1 :(得分:0)

您需要在 File App.xaml 中插入文件的方向.. 以我为例:

<Application x:Class="VBControl.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:VBControl"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\StringResources.xaml"/>
            <ResourceDictionary Source="Resources\StringResources.en-EN.xaml" />
            <ResourceDictionary Source="Resources\StringResources.pt-BR.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>