如何自动保存 RichEditBox 文本?

How to autosave RichEditBox text?

我尝试 FileIO.WriteTextAsync(); 处理许多事件,包括 Grid.Loaded 和 RichEditBox.TextChanged,但该方法是异步的,并且与调用上一个循环的过程冲突。结果,System.IO.FileLoadException 具有以下描述:

The process cannot access the file because it is being used by another process.

在这种情况下我能做什么?

TextChanged 事件触发过于频繁 - 您可以在一秒钟内键入多个字符 - 处理 TextChanged 事件对于 UI 线程来说太繁重了。

您可以在 Timer Tick 事件处理程序中执行此保存操作,设置足够长的间隔以确保在下一个 Tick 发生之前完成操作。

DispatcherTimer timer; 

public MainPage()
{
    this.InitializeComponent();

    timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(10) };
    timer.Tick += Timer_Tick;
    timer.Start();
}

private async void Timer_Tick(object sender, object e)
{
    await FileIO.WriteTextAsync(file, tbInput.Text);
}