C# 窗口上多个控件的Syncfusion WPF拼写检查

C# 窗口上多个控件的Syncfusion WPF拼写检查,c#,wpf,spell-checking,syncfusion,C#,Wpf,Spell Checking,Syncfusion,我正在考虑将我的拼写检查实现更改为使用Syncfusion拼写检查。我已经让它大部分工作,但有一个问题和一个问题。首先是我的测试应用程序代码。我按照示例代码进行了操作,然后我想将其应用到同一窗口上的多个文本框中,这就是我实际应用程序中的情况。上次我尝试激活自定义词典 以下是我的应用程序窗口XAML代码: <Window x:Class="SyncfusionWpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2

我正在考虑将我的拼写检查实现更改为使用Syncfusion拼写检查。我已经让它大部分工作,但有一个问题和一个问题。首先是我的测试应用程序代码。我按照示例代码进行了操作,然后我想将其应用到同一窗口上的多个文本框中,这就是我实际应用程序中的情况。上次我尝试激活自定义词典

以下是我的应用程序窗口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);
        }
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用Syncfusion.Windows.Controls;
命名空间SyncfusionWpfApp1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
}
已加载无效tb_(对象发送器、路由目标)
{
var spellChecker=新的SfSpellChecker
{
自定义字典路径=
@“C:\Users\sfaust\Source\Repos\SFTest1\SyncfusionWpfApp1\SyncfusionWpfApp1\bin\Debug\testdictionary.txt”
};
var textBoxSpellEditor=新的textBoxSpellEditor((TextBox)发送方);
拼写检查器。使用ContextMenu(textBoxSpellEditor)执行拼写检查;
}
}
}
好的,第一个问题。当我移动到多个控件时,它似乎无法正常工作,除非我为每个文本框创建一个新的拼写检查器和新的IEditorProperty(TextBoxSpellEditor类)并应用它(因此在上面加载的控件中初始化它,而不是像示例那样在窗口中初始化它)。嗯,这似乎奏效了,而且效率也很低。我的应用程序中可能会有相当多的文本框,因为它们位于项目的树状视图中,所以我有点担心实例化数百个拼写检查的效率(尽管我没有尝试压力测试,所以可能我担心不必要)。这是正确的方法吗


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

感谢您联系Syncfusion支持部门

问题1:一旦我移动到多个控件,除非我创建一个新的拼写检查器和新的
ieditorproperty

不必每次都为
SfSpellChecker
创建新实例,您可以在
ControlToSpellCheck
中传递当前文本框控件,该控件是控件属性。之后,您可以使用ContextMenu调用
PerformsPellCheck。因此,您可以通过单个实例获得拼写检查结果

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
中,我们目前不直接支持从应用程序外部加载字典文件。如果您想将资源文件加载到不同的位置,我们可以通过使用反射来实现这一点,并根据给定的代码段将字典分配给拼写检查器

例如,如果此位置存在自定义词典。(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  ) );