Windows 11下WPF SpellCheck自定义词典失效问题求助
Windows 11下WPF SpellCheck自定义词典不生效问题解决
问题现象
在Windows 10中正常运行的WPF拼写检查自定义词典逻辑,迁移到Windows 11后出现异常:系统级拼写检查正常,但添加到自定义词典的单词仍被标记为拼写错误。
相关代码及文件信息:
- XAML中的TextBox:
<TextBox Name="SpellCheckTextBox" Grid.Column="1" Height="20" Margin="0 0 5 0" VerticalAlignment="Center" SpellCheck.IsEnabled="True" />
- 添加自定义词典的测试代码:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { var dictionaries = SpellCheck.GetCustomDictionaries(SpellCheckTextBox); dictionaries.Add(new Uri("file:///C:/Temp//TestApp/Dictionaries/dictionary_de-DE.lex")); }
- 词典文件
dictionary_de-DE.lex内容:
thisIsATest secondTest
解决方法
针对Windows 11的特性差异,按以下步骤排查修复:
修正词典文件编码
Windows 11对.lex自定义词典的编码要求严格,必须使用**UTF-16 LE(带BOM)**格式。可以通过记事本打开词典文件,选择「另存为」,在编码下拉框中选择「Unicode」保存即可。优化URI路径写法
代码中路径的双斜杠//可能导致解析异常,建议改用单斜杠,或使用应用内相对路径(需确保词典文件在项目中设置为「复制到输出目录:始终复制」):
// 应用内相对路径示例 dictionaries.Add(new Uri("pack://application:,,,/Dictionaries/dictionary_de-DE.lex"));
调整文件权限与存放位置
Windows 11的权限控制更严格,避免将词典放在C盘根目录的Temp文件夹,建议移至应用安装目录或用户AppData文件夹,确保程序拥有读取权限。手动触发拼写检查刷新
添加词典后强制刷新TextBox的拼写检查状态,确保新词典生效:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { var dictionaries = SpellCheck.GetCustomDictionaries(SpellCheckTextBox); dictionaries.Add(new Uri("pack://application:,,,/Dictionaries/dictionary_de-DE.lex")); // 刷新拼写检查 SpellCheckTextBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource(); SpellCheckTextBox.ClearValue(SpellCheck.IsEnabledProperty); SpellCheckTextBox.SetValue(SpellCheck.IsEnabledProperty, true); }
- 匹配TextBox的语言设置
确保TextBox的Language属性与词典的语言后缀一致,比如德语词典对应de-DE:
<TextBox Name="SpellCheckTextBox" Grid.Column="1" Height="20" Margin="0 0 5 0" VerticalAlignment="Center" Language="de-DE" SpellCheck.IsEnabled="True" />
内容的提问来源于stack exchange,提问作者simmeone
相关产品推荐
相关产品推荐

