【问题标题】:Syncfusion WPF Spell Check for Multiple Controls on Window用于窗口上多个控件的 Syncfusion WPF 拼写检查
【发布时间】:2020-05-29 15:10:44
【问题描述】:

我正在考虑更改我的拼写检查实现以使用 Syncfusion 拼写检查。我大部分时间都在工作,但有一个问题和一个问题。首先是我的测试应用程序代码。我按照示例代码here 进行操作并使其正常工作,然后我想将其应用于同一窗口上的多个文本框,就像在我的实际应用程序中那样。最后我尝试激活自定义字典。

这是我的应用窗口 XAML 代码:

<Window x:Class="SyncfusionWpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Icon="App.ico">
<Grid>
    <StackPanel>
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
    </StackPanel>
</Grid>

下面是代码:

using System.Windows;
using System.Windows.Controls;
using Syncfusion.Windows.Controls;

namespace SyncfusionWpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void tb_Loaded(object sender, RoutedEventArgs e)
        {
            var spellChecker = new SfSpellChecker
            {
                CustomDictionaryPath =
                    @"C:\Users\sfaust\Source\Repos\SFTest1\SyncfusionWpfApp1\SyncfusionWpfApp1\bin\Debug\testdictionary.txt"
            };
            var textBoxSpellEditor = new TextBoxSpellEditor((TextBox)sender);
            spellChecker.PerformSpellCheckUsingContextMenu(textBoxSpellEditor);
        }
    }
}

好的,第一个问题。一旦我移动到多个控件,它似乎无法正常工作,除非我为每个文本框创建一个新的拼写检查器和新的 IEditorProperties(TextBoxSpellEditor 类)并应用它(因此在上面加载而不是在窗口中初始化它,如例子)。嗯,这似乎工作它似乎也相当低效。我的应用程序可能会在其中包含相当多的文本框,因为它们位于项目的树视图中,所以我有点担心实例化可能有数百个拼写检查器的效率(尽管我没有尝试过压力测试所以也许我不必要地担心)。这是正确的方法吗?

第二个问题更重要。我没有看到任何表明自定义词典正在工作的东西。我在显示的路径中创建了文件,并在其中放入了一些随机“单词”,它(正确地)识别为拼写错误,但即使将它们放入该文件并设置自定义字典路径属性,它仍然将它们识别为拼写错误。我也没有上下文菜单上的“AddToDictionary”选项。我还尝试只设置属性而不实际创建文件,以防它想创建文件本身但没有更改。最后我尝试了相对路径和绝对路径,但也没有改变。关于如何激活自定义词典,我有什么遗漏吗?

【问题讨论】:

    标签: c# wpf spell-checking syncfusion


    【解决方案1】:

    感谢您联系 Syncfusion 支持。

    查询 1:一旦我移动到多个控件,它似乎无法正常工作,除非我创建一个新的拼写检查器和新的 IEditorProperties

    您可以在ControlToSpellCheck 中传递当前文本框控件,而不是每次都为SfSpellChecker 创建新实例,这是一个控件属性。之后您可以拨打PerformSpellCheckUsingContextMenu。因此,您可以通过单个实例获取拼写检查结果。

    private void OnGotFocus(object sender, RoutedEventArgs e) 
        { 
            TextBox textBox = sender as TextBox; 
            if (this.SpellEditor == null) 
            { 
                SpellEditor = new TextSpellEditor(textBox); 
            } 
            else 
                SpellEditor.ControlToSpellCheck = textBox; 
            SpellChecker.PerformSpellCheckUsingContextMenu(SpellEditor); 
        }
    

    查询 2:我没有看到任何表明自定义词典正在工作的信息

    SfSpellChecker 中,我们目前不直接支持从应用程序外部加载字典文件。如果你想加载资源文件不同的位置,我们可以通过使用反射来实现这一点,并按照给定的代码 sn-p 将字典分配给拼写检查器。

    例如,如果此位置存在自定义词典。(D:\CustomDictionary\CustomDict.txt)

    代码示例[C#]

    Stream fileStream = new FileStream(@"D:\CustomDictionary\CustomDict.txt", FileMode.Open); 
    spellChecker.GetType().GetField("checker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(spellChecker, new SpellCheckerBase(fileStream  ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多