【问题标题】:How to autosave RichEditBox text?如何自动保存 RichEditBox 文本?
【发布时间】:2018-07-19 05:29:06
【问题描述】:
我尝试FileIO.WriteTextAsync(); 处理许多事件,包括 Grid.Loaded 和 RichEditBox.TextChanged,但该方法是异步的,并且与称为上一个循环的进程冲突。结果,System.IO.FileLoadException 具有以下描述:
该进程无法访问该文件,因为它正被另一个进程使用。
那么在这种情况下我该怎么办?
【问题讨论】:
标签:
uwp
autosave
fileloadexception
richeditbox
【解决方案1】:
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);
}