0

嗨,我需要制作多语言 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>

加载时出现错误:系统无效操作异常:操作错误 loadfrom resouceDictionary with URI:"file:///c:

感谢您的回复

4

2 回答 2

0

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

于 2013-07-03T06:54:23.947 回答
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>
于 2020-12-28T22:32:17.130 回答