You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server 2019及Win10新版本中WPF自定义词典失效问题求助

问题确认

你遇到的是新版Windows(Win10 1809及以上、Windows Server 2019)与.NET 4.5 WPF SpellCheck自定义词典的兼容性问题——微软在这些系统中更新了拼写检查的底层架构,而.NET 4.5的SpellCheck API未适配该变化,导致自定义词典加载失效。


无第三方控件的修复方案

方案1:启用旧版拼写检查逻辑(推荐)

通过修改应用的app.config文件,添加AppContext开关强制WPF使用旧版拼写检查实现,无需修改代码即可生效:
在配置文件的<configuration>节点内添加以下内容:

<runtime>
  <AppContextSwitchOverrides value="Switch.System.Windows.Controls.RichTextBox.UseOldSpellCheckLogic=true" />
</runtime>

该开关是微软为兼容旧应用专门提供的,会让RichTextBox调用与Windows Server 2016/Win10 1670一致的拼写检查模块,确保自定义词典正常加载。

方案2:修正自定义词典路径

新版Windows对文件路径的权限和解析逻辑更严格,检查DictionaryUri是否符合以下要求:

  • 使用绝对路径而非相对路径,示例:
    var dictionaryUri = new Uri(@"C:\YourApp\Resources\custom.dic", UriKind.Absolute);
    
  • 如果词典是嵌入应用的资源,使用Pack URI格式,示例:
    var dictionaryUri = new Uri("pack://application:,,,/YourAppAssembly;component/Resources/custom.dic", UriKind.Absolute);
    
  • 确保应用对词典文件所在目录有读取权限(避免将文件放在Program Files等受系统保护的目录)

方案3:手动刷新拼写检查上下文(进阶)

如果上述方案无效,可通过反射触发SpellCheck内部的上下文更新逻辑,确保词典被正确加载:

using System.Reflection;

public void InitDictionary()
{
    var dictionaries = SpellCheck.GetCustomDictionaries(richText);
    if (dictionaries.Count == 0)
    {
        var dictionaryUri = TextBoxDictionary.DictionaryUri;
        if (dictionaryUri != null)
        {
            dictionaries.Add(dictionaryUri);
            
            // 反射调用内部更新方法
            var spellCheckType = typeof(SpellCheck);
            var updateMethod = spellCheckType.GetMethod("UpdateSpellCheckContext", BindingFlags.NonPublic | BindingFlags.Static);
            updateMethod?.Invoke(null, new object[] { richText });
        }
    }
}

内容的提问来源于stack exchange,提问作者Ramanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 13:10:13