diff --git a/Ink Canvas/MainWindow.xaml b/Ink Canvas/MainWindow.xaml index 58f62b04..5e6a2e08 100644 --- a/Ink Canvas/MainWindow.xaml +++ b/Ink Canvas/MainWindow.xaml @@ -5538,7 +5538,7 @@ - + /// 安全地更新临时笔画,减少预览闪烁 + /// + /// 新的临时笔画 + private void UpdateTempStrokeSafely(Stroke newStroke) + { + // 节流机制:限制更新频率 + var now = DateTime.Now; + if ((now - lastUpdateTime).TotalMilliseconds < UpdateThrottleMs) + { + return; + } + lastUpdateTime = now; + + try + { + // 使用Dispatcher.BeginInvoke确保UI更新在UI线程上执行 + Dispatcher.BeginInvoke(new Action(() => + { + try + { + // 先添加新笔画,再删除旧笔画,减少视觉闪烁 + inkCanvas.Strokes.Add(newStroke); + + if (lastTempStroke != null && inkCanvas.Strokes.Contains(lastTempStroke)) + { + inkCanvas.Strokes.Remove(lastTempStroke); + } + + lastTempStroke = newStroke; + } + catch (Exception ex) + { + Debug.WriteLine($"UpdateTempStrokeSafely 失败: {ex.Message}"); + // 如果更新失败,确保清理状态 + if (lastTempStroke != null && inkCanvas.Strokes.Contains(lastTempStroke)) + { + try { inkCanvas.Strokes.Remove(lastTempStroke); } catch { } + } + lastTempStroke = newStroke; + try { inkCanvas.Strokes.Add(newStroke); } catch { } + } + }), DispatcherPriority.Render); + } + catch (Exception ex) + { + Debug.WriteLine($"UpdateTempStrokeSafely Dispatcher 失败: {ex.Message}"); + } + } + + /// + /// 安全地更新临时笔画集合,减少预览闪烁 + /// + /// 新的临时笔画集合 + private void UpdateTempStrokeCollectionSafely(StrokeCollection newStrokeCollection) + { + // 节流机制:限制更新频率 + var now = DateTime.Now; + if ((now - lastUpdateTime).TotalMilliseconds < UpdateThrottleMs) + { + return; + } + lastUpdateTime = now; + + try + { + // 使用Dispatcher.BeginInvoke确保UI更新在UI线程上执行 + Dispatcher.BeginInvoke(new Action(() => + { + try + { + // 先添加新笔画集合,再删除旧笔画集合,减少视觉闪烁 + inkCanvas.Strokes.Add(newStrokeCollection); + + if (lastTempStrokeCollection != null && lastTempStrokeCollection.Count > 0) + { + foreach (var stroke in lastTempStrokeCollection) + { + if (inkCanvas.Strokes.Contains(stroke)) + { + inkCanvas.Strokes.Remove(stroke); + } + } + } + + lastTempStrokeCollection = newStrokeCollection; + } + catch (Exception ex) + { + Debug.WriteLine($"UpdateTempStrokeCollectionSafely 失败: {ex.Message}"); + // 如果更新失败,确保清理状态 + if (lastTempStrokeCollection != null && lastTempStrokeCollection.Count > 0) + { + foreach (var stroke in lastTempStrokeCollection) + { + try { inkCanvas.Strokes.Remove(stroke); } catch { } + } + } + lastTempStrokeCollection = newStrokeCollection; + try { inkCanvas.Strokes.Add(newStrokeCollection); } catch { } + } + }), DispatcherPriority.Render); + } + catch (Exception ex) + { + Debug.WriteLine($"UpdateTempStrokeCollectionSafely Dispatcher 失败: {ex.Message}"); + } + } + private List GenerateEllipseGeometry(Point st, Point ed, bool isDrawTop = true, bool isDrawBottom = true) {