WPF .NET 4.0 контроль проверки правописания при вводе? - PullRequest
2 голосов
/ 16 июня 2011

Я ищу элемент управления проверкой орфографии в WPF (похожий на встроенную функциональность, но с поддержкой большего количества словарей). Мне нужно, чтобы он имел функциональность типа «как вы» (подчеркивание красного цвета). Он также должен поддерживать более 4 языков, чем встроенная проверка орфографии .NET 4.0 (например, поддержка английского, испанского, немецкого, итальянского и русского языков будет отличной).

Я предпочитаю, чтобы у элемента управления была лицензия MIT или BSD, которую можно использовать в коммерческом приложении Windows. Исходный код был бы великолепен, так как я хотел бы интегрировать предложения по написанию в мое пользовательское контекстное меню правой кнопкой мыши.

Ответы [ 2 ]

1 голос
/ 23 апреля 2015

Я объединил AvalonEdit с NHunspell , добавив свой собственный SpellCheckerBehavior. Пример проекта этого может быть найден в github . Это реализовано так:

<avalonEdit:TextEditor
    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
    Name="editor">
    <avalonEdit:TextEditor.ContextMenu>
        <ContextMenu>
            <MenuItem Command="Undo" />
            <MenuItem Command="Redo" />
            <Separator/>
            <MenuItem Command="Cut" />
            <MenuItem Command="Copy" />
            <MenuItem Command="Paste" />
        </ContextMenu>
    </avalonEdit:TextEditor.ContextMenu>
    <i:Interaction.Behaviors>
        <local:SpellCheckerBehavior />
    </i:Interaction.Behaviors>
</avalonEdit:TextEditor>

... изменив конструктор моего MainWindow следующим образом:

public MainWindow()
{
    SpellChecker.Default.HunspellInstance = new Hunspell("German.aff", "German.dic");
    editor.TextArea.TextView.LineTransformers.Add(new SpellCheckerColorizer());
}

... и добавление следующих классов в мой проект для их интеграции:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
using NHunspell;

namespace Martin
{
    public class SpellChecker
    {
        static Lazy<SpellChecker> defaultInstance = new Lazy<SpellChecker>(() => new SpellChecker());
        public static SpellChecker Default { get { return defaultInstance.Value; } }

        public Hunspell HunspellInstance { get; set; }

        public class Word
        {
            public int Index { get; set; }
            public string Value { get; set; }
        }

        static IEnumerable<Word> FindWords(string text)
        {
            foreach (Match m in new Regex(@"\w+").Matches(text))
            {
                yield return new Word() { Index = m.Index, Value = m.Value };
            }
        }

        public IEnumerable<Word> FindSpellingErrors(string text)
        {
            foreach (var word in FindWords(text))
            {
                if (!Spell(word.Value))
                {
                    yield return word;
                }
            }
        }

        public bool Spell(string word)
        {
            return HunspellInstance.Spell(word);
        }

        public List<string> Suggest(string word)
        {
            return HunspellInstance.Suggest(word);
        }
    }

    public class SpellCheckerBehavior : Behavior<TextEditor>
    {
        TextEditor textEditor;
        List<Control> originalItems;

        protected override void OnAttached()
        {
            textEditor = AssociatedObject;
            if (textEditor != null)
            {
                textEditor.ContextMenuOpening += new ContextMenuEventHandler(TextEditorContextMenuOpening);
                textEditor.TextArea.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(TextAreaMouseRightButtonDown);
                originalItems = textEditor.ContextMenu.Items.OfType<Control>().ToList();
            }
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            if (textEditor != null)
            {
                textEditor.ContextMenuOpening -= new ContextMenuEventHandler(TextEditorContextMenuOpening);
                textEditor.TextArea.MouseRightButtonDown -= new System.Windows.Input.MouseButtonEventHandler(TextAreaMouseRightButtonDown);
                originalItems = null;
                textEditor = null;
            }
            base.OnDetaching();
        }

        void TextAreaMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var position = textEditor.GetPositionFromPoint(e.GetPosition(textEditor));
            if (position.HasValue)
            {
                textEditor.TextArea.Caret.Position = position.Value;
            }
        }

        void TextEditorContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            foreach (Control item in textEditor.ContextMenu.Items.OfType<Control>().ToList())
            {
                if (originalItems.Contains(item)) { continue; }
                textEditor.ContextMenu.Items.Remove(item);
            }
            var position = textEditor.TextArea.Caret.Position;
            Match word = null;
            Regex r = new Regex(@"\w+");
            var line = textEditor.Document.GetText(textEditor.Document.GetLineByNumber(position.Line));
            foreach (Match m in r.Matches(line))
            {
                if (m.Index >= position.VisualColumn) { break; }
                word = m;
            }
            if (null == word ||
                position.Column > word.Index + word.Value.Length ||
                SpellChecker.Default.Spell(word.Value))
            {
                return;
            }
            textEditor.ContextMenu.Items.Insert(0, new Separator());
            var suggestions = SpellChecker.Default.Suggest(word.Value);
            if (0 == suggestions.Count)
            {
                textEditor.ContextMenu.Items.Insert(0,
                    new MenuItem() { Header = "<No suggestions found>", IsEnabled = false });
                return;
            }
            foreach (string suggestion in suggestions)
            {
                var item = new MenuItem { Header = suggestion, FontWeight = FontWeights.Bold };
                item.Tag =
                    new Tuple<int, int>(
                        textEditor.Document.GetOffset(position.Line, word.Index + 1),
                        word.Value.Length);
                item.Click += ItemClick;
                textEditor.ContextMenu.Items.Insert(0, item);
            }
        }

        private void ItemClick(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            if (null == item) { return; }
            var segment = item.Tag as Tuple<int, int>;
            if (null == segment) { return; }
            textEditor.Document.Replace(segment.Item1, segment.Item2, item.Header.ToString());
        }
    }

    public class SpellCheckerColorizer : DocumentColorizingTransformer
    {
        private readonly TextDecorationCollection textDecorationCollection;

        public SpellCheckerColorizer()
        {
            textDecorationCollection = new TextDecorationCollection();
            textDecorationCollection.Add(new TextDecoration()
            {
                Pen = new Pen { Thickness = 1, DashStyle = DashStyles.Dot, Brush = new SolidColorBrush(Colors.Red) },
                PenThicknessUnit = TextDecorationUnit.FontRecommended
            });
        }

        protected override void ColorizeLine(DocumentLine line)
        {
            var lineText = CurrentContext.Document.Text
                .Substring(line.Offset, line.Length);
            foreach (var error in SpellChecker.Default.FindSpellingErrors(lineText))
            {
                base.ChangeLinePart(line.Offset + error.Index, line.Offset + error.Index + error.Value.Length,
                            (VisualLineElement element) => element.TextRunProperties.SetTextDecorations(textDecorationCollection));
            }
        }
    }
}
0 голосов
/ 16 июня 2011

AvalonEdit (http://www.codeproject.com/KB/edit/AvalonEdit.aspx) был написан с нуля в WPF для SharpDevelop.О, чудеса быстрого Google.

И если у вас все в порядке с одним языком за раз (но с возможностью переключения), вы можете использовать функциональность WPF по умолчанию (http://joshsmithonwpf.wordpress.com/2007/02/17/spellchecking-and-suggested-spellings-in-a-textbox/)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...