0

我在我的 WPF 应用程序中使用标准的主题方法,并在运行时使用动态资源来更改它。

这行得通。

现在我添加了语言支持(英语、西班牙语),但设置后,动态资源不再适用于主题。如果我将控件设置为静态资源,则主题可以工作,但是我会失去在运行时更改它的影响。

这是为语言支持添加的代码,但我很困惑为什么它停止按我想要的方式工作。

App.xaml.cs 称之为:SetLanguage(LocalePath(CultureInfo.CurrentCulture.Name));

/// <summary>
    /// Get the locale path for the resource dictionary.
    /// </summary>
    /// <param name="language"></param>
    /// <returns></returns>
    private string LocalePath(string language)
    {
        string file = language + ".xaml";

        return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Utilities\Resources\Locales\", file);
    }

    /// <summary>
    /// Set the language using the defined resource file.
    /// </summary>
    /// <param name="file"></param>
    private void SetLanguage(string file)
    {
        if (File.Exists(file))
        {
            var languageDictionary = new ResourceDictionary();
            languageDictionary.Source = new Uri(file);

            int dictionaryID = -1;

            for (int i = 0; i < Resources.MergedDictionaries.Count; i++)
            {
                var mergedDictionary = Resources.MergedDictionaries[i];

                if (mergedDictionary.Contains("ResourceDictionaryName"))
                {
                    if (mergedDictionary["ResourceDictionaryName"].ToString().StartsWith("Locale-"))
                    {
                        dictionaryID = i;
                        break;
                    }
                }
            }

            if (dictionaryID == -1)
            {
                Resources.MergedDictionaries.Add(languageDictionary);
            }
            else
            {
                Resources.MergedDictionaries[dictionaryID] = languageDictionary;
            }
        }
    }
4

1 回答 1

0

我在语言资源字典中有 5 个其他键,它们与所使用的主题相同。更改了密钥名称,它又可以工作了。

于 2012-05-09T21:09:22.130 回答