c# - WPF 自定义词典仅在 Windows 10 上的 %TEMP% 中创建 .dict 文件

标签 c# .net wpf

我遇到一个问题,即当我将自定义词典添加到文本框/richtextbox 时,在 %TEMP% 中创建“.dic”文件并且未将其删除。这在 Windows 7 中运行良好,但现在在 Win 10 上我们遇到了上述问题。这些文件始终为零字节,名称类似于 1s3iqqvw.dic。

真正的应用程序有大量的文本/富文本框,每次实例化时,添加自定义词典时都会创建一个新的 dic 文件,用户很快就会在临时目录中出现数百个这样的文件。

更糟糕的是,随着创建的 .dic 文件越来越多,该过程会变得越来越慢 - Windows 似乎会生成随机文件名,然后在创建文件名之前在目录中搜索重复文件,如此重复,直到找到唯一的文件名。当 .dic 文件有数百个时,这会大大减慢应用程序的速度 - 我们需要手动删除 .dic 来修复。

以下代码将重现该问题。在 Win 10 上,.dic 文件是在 %TEMP% 中创建的 - Win 7 上不是这种情况

{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //dict.txt must exist
            this.textBox1.SpellCheck.CustomDictionaries.Add(new Uri("d:\\dict.txt", UriKind.Relative));
            textBox1.Text = "Lorem ipsum dolor sit amet";
        }
    }
}

<Window x:Class="testapp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:u="clr-namespace:testapp"
        Title="MainWindow" Height="350" Width="799"
        >
    <Grid>
        <TextBox SpellCheck.IsEnabled="True" Height="287" Margin="6,12,0,0" Name="textBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="759" TextWrapping="Wrap" AcceptsReturn="True"/>
    </Grid>
</Window>

有人知道 Windows 10 上的这个问题吗?你有解决办法吗?

最佳答案

我知道这并不能真正解决问题,但目前看来至少有帮助。我找不到避免创建 .dic 文件的方法。但是,查看代码我发现清除 CustomDictionaries 会触发 .dic 文件被删除。

我目前的想法是在窗口关闭时清除它们。您可以手动清除它们,也可以循环遍历它们,如下所示。 (感谢帖子Find all controls in WPF Window by type)

    public MainWindow()
    {
        InitializeComponent();

        this.textBox1.SpellCheck.CustomDictionaries.Add(new Uri("C:\\dict.txt", UriKind.Relative));
        textBox1.Text = "Lorem ipsum dolor sit amet";

        this.Closed += ((object sender, EventArgs e) => {
            foreach (var txtbox in FindVisualChildren<TextBox>(this))
            {
                txtbox?.SpellCheck?.CustomDictionaries?.Clear();
            }
        });
    }

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

如果您有兴趣,我认为造成问题的源代码可以在这里找到: ( https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Documents/WinRTSpellerInterop.cs,2777e92ca38df44a,references )

关于c# - WPF 自定义词典仅在 Windows 10 上的 %TEMP% 中创建 .dict 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59684040/

相关文章:

c# - 检查枚举标志的最佳实践

c# - 如何取消选择按钮 `Select all` 而不是所有项目?

c# - "value cannot be null. parameter name container"xaml

C#计算给定宽度的字符串的高度

.net - 无法使用 HTTPS 使 ServiceStack 在 IIS6 中工作

.net - Visual Studio 2010最大化代码窗口快捷方式

c# - 更正 .NET 中 "mixed"类型的 XML 序列化和反序列化

c# - 扩展器 header 的宽度不正确且文本已旋转

wpf - 从文本框中过滤WPF DataGrid值

c# - Visual Studio 中 Web 应用程序的控制台输出