WPF RichTextBox SpellCheck ComException

Eug*_*voy 10 c# wpf spell-checking richtextbox windows-runtime

我试图在某些Windows 8.1机器上启用拼写检查时遇到异常(两者都有最新更新,操作系统语言是俄语,.NET框架4.7是俄语)说:

System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.Runtime.InteropServices.COMException:Windows.Data.Text.WordsSegmenter..ctor处的System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)中的注册表值(来自HRESULT的异常:0x80040153(REGDB_E_INVALIDVALUE))无效()字符串语言)---内部异常堆栈跟踪的结束---在System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr,Binder binder)的System.RuntimeMethodHandle.InvokeMethod(Object target,Object []参数,Signature sig,Boolean构造函数)中,对象[]参数,CultureInfo文化)在MS.Internal.WindowsRuntime.Windows.Data.Text.WordsSegmenter..ctor(String language)的MS.Internal.WindowsRuntime.ReflectionHelper.ReflectionNew [TArg1](类型类型,TArg1 arg1)在Syst的System.Windows.Documents.WinRTSpellerInterop.EnsureWordBreakerAndSpellCheckerForCulture(CultureInfo culture,Boolean throwOnError)的MS.Internal.WindowsRuntime.Windows.Data.Text.WordsSegmenter.Create(String language,Boolean shouldPreferNeutralSegmenter)系统.Windows.Documents.WinRTSpellerInterop..ctor()在System.Windows.Documents.SpellerInteropBase.CreateInstance()处于System.Windows.Documents.Speller.EnsureInitialized()处于System.Windows.Documents.Speller.SetCustomDictionaries(CustomDictionarySources dictionaryLocations, System.Windows上的System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)上的System.Windows.Controls.SpellCheck.OnIsEnabledChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)中的System.Windows.Documents.TextEditor.SetCustomDictionaries(Boolean add)处的布尔添加)在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs参数)在System.Windows.DependencyObject.UpdateEffectiveValue(entryIndex entryIndex,的DependencyProperty DP,PropertyMetadata元数据,EffectiveValueEntry oldEntry,EffectiveValueEntry&newEntry,布尔coerceWithDeferredReference,布尔coerceWithCu .FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs E)System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)中的System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,Object value,PropertyMetadata metadata,Boolean coerceWithDeferredReference,Boolean coerceWithCurrentValue,OperationType operationType,Boolean isInternal)中的rrentValue,OperationType operationType)

此代码可用于重现该问题:

var richTextBox = new RichTextBox();
InputLanguageManager.SetInputLanguage(richTextBox,CultureInfo.GetCultureInfo("en-US"));
richTextBox.SetValue(SpellCheck.IsEnabledProperty, true);
Run Code Online (Sandbox Code Playgroud)

在研究这个问题时,我发现异常是从s_WinRTType描述类型"Windows.Data.Text.WordsSegmenter,Windows,ContentType = WindowsRuntime.WindowsSegmenter似乎是WinRT组件的s_WinRTType.ReflectionNew<string>(language);地方抛出的,所以我看不到里面发生了什么.我想要要知道它为什么抛出REGDB_E_INVALIDVALUE /它寻找的值以及它应该是什么样的?谢谢!

更新1.我还看到该组件的密钥存在于注册表中: 在此输入图像描述 所以这个组件可能会抛出异常

Wou*_*ter 0

您需要通过控制面板或使用 DISM 安装与语言包不同的语言功能。对我来说,这需要 .Net 4.7,并且适用于 Windows 10 build 1709(秋季创建者更新)。我不知道这在 Windows 8 上是否可行。

如果您能够正确访问 Windows 更新(不在 WSUS 之后),您可以尝试安装它

Dism /Online /Add-Capability /CapabilityName:Language.Basic~~~en-US~0.0.1.0

要检查已安装的功能,这会显示所有已安装的选项:

dism /在线/获取功能/ limitaccess

背景资料:

https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/add-language-packs-to-windows

https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/dism-capability-package-servicing-command-line-options

https://blogs.technet.microsoft.com/mniehaus/2015/08/31/adding-features-include-net-3-5-to-windows-10/

最后一个链接解释了按需提供另一个版本 2 的功能。对我来说它解决了一个问题。

使用按需功能 isos(从您的 msdn 订阅下载):

对于 Windows 10(不知道这是否适用于 Windows 8): en_windows_10_features_on_demand_part_1_version_1709_updated_sept_2017_x64_dvd_100090755 en_windows_10_features_on_demand_part_2_version_1709_updated_sept_2017_x64_dvd_100 090754

提取它们并安装:

dism /online /add-package /packagepath:d:\features\Microsoft-Windows-LanguageFeatures-Basic-en-us-Package.cab

PS:您是否 Google 搜索 REGDB_E_INVALIDVALUE:VSHost 崩溃,REGDB_E_INVALIDVALUE 加载特定项目

例子:

您可以使用以下代码创建一个 wpf 测试应用程序: 这将读取可以使用的AvailableInputLanguages。(对我来说,它不显示 .Net 4 中的 4 种语言,只显示我用 Dism 安装的语言。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ComboBox 
          ItemsSource="{Binding AvailableLanguages}"
          SelectionChanged="OnLanguageSelectionChanged"
          DisplayMemberPath="NativeName"/>
    <TextBox x:Name="textBox" Grid.Row="1"
         AcceptsReturn="True"
         AcceptsTab="True"
         SpellCheck.IsEnabled="True"
         Text="Hello world"/>
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        AvailableLanguages = new ObservableCollection<CultureInfo>();

        foreach (CultureInfo culterInfo in InputLanguageManager.Current.AvailableInputLanguages)
        {
            AvailableLanguages.Add(culterInfo);
        }

        DataContext = this;
    }

    public ObservableCollection<CultureInfo> AvailableLanguages
    {
        get { return (ObservableCollection<CultureInfo>)GetValue(AvailableLanguagesProperty); }
        set { SetValue(AvailableLanguagesProperty, value); }
    }

    public static readonly DependencyProperty AvailableLanguagesProperty = DependencyProperty.Register("AvailableLanguages", typeof(ObservableCollection<CultureInfo>), typeof(MainWindow));


    private void OnLanguageSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        CultureInfo xmlLanguage = e.AddedItems[0] as CultureInfo;
        textBox.Language = XmlLanguage.GetLanguage(xmlLanguage.Name);
    }
}
Run Code Online (Sandbox Code Playgroud)