WPF Richtextbox中的自动更正

时间:2015-12-17 12:53:34

标签: c# .net wpf .net-4.6.1

我在MSDN上读到.NET 4.6.1现在支持自动更正。 %appdata%/ Microsoft / Spelling //中的文件是自动创建的,我将以下行添加到default.acl(文件仍然是带有BOM的UTF-16):

tramampoline|trampoline

我已将项目设置为目标4.6.1并在RichTextBox上启用了SpellCheck:

<RichTextBox SpellCheck.IsEnabled="True" Language="de-DE"/>

虽然它以通常的方式突出显示错误的单词,但没有发生自动更正。

我在这里缺少什么? 我不太明白这个说明:

  

注意:WPF拼写检查API不直接支持这些新的文件格式,应用程序中提供给WPF的自定义词典应继续使用.lex文件。

1 个答案:

答案 0 :(得分:0)

我知道这很旧,但是据我所知,您需要自己处理自动更正(如果我错了,请举个例子来纠正我)。您可以按照以下步骤进行操作:

var caretPosition = richTextBox.CaretPosition;
// Make sure you're passing a textpointer at the end of the word you want to correct, i.e. not like this ;)
errorPosition = richTextBox.GetNextSpellingErrorPosition(caretPosition, LogicalDirection.Backward);
if(errorPosition == null)
{
    return;
}

var errors = richTextBox.GetSpellingError(errorPosition);
// Default would be to only replace the text if there is one available replacement
// but you can also measure which one is most likely with a simple string comparison
// algorithm, e.g. Levenshtein distance
if (errors.Suggestions.Count() == 1) 
{
    var incorrectTextRange = richTextBox.GetSpellingErrorRange(errorPosition);
    var correctText = error.Suggestions.First();
    var incorrectText = incorrectTextRange.Text;

    // Correct the text with the chosen word...
    errors.Correct(correctText);
}

// Set caret position...

一个重要的注意事项是不要使用RTB的CaretPosition,而应在要纠正的单词的末尾使用文本指针。如果您的文本指针/插入符号很奇怪(例如20个空格的末尾),则GetNextSpellingErrorPosition方法最多可能需要60秒才能返回(取决于硬件/ RTB中的单词数)。

相关问题