diff --git a/Ink Canvas/MainWindow_cs/MW_BoardControls.cs b/Ink Canvas/MainWindow_cs/MW_BoardControls.cs index e029ab60..9c05e70c 100644 --- a/Ink Canvas/MainWindow_cs/MW_BoardControls.cs +++ b/Ink Canvas/MainWindow_cs/MW_BoardControls.cs @@ -7,6 +7,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Ink; using System.Windows.Media; +using System.Windows.Threading; namespace Ink_Canvas { @@ -247,15 +248,39 @@ namespace Ink_Canvas { timeMachine.ImportTimeMachineHistory(TimeMachineHistories[0]); var removed0 = CollectRemovedElementsFromHistory(TimeMachineHistories[0]); - foreach (var item in TimeMachineHistories[0]) ApplyHistoryToCanvas(item, null, removed0); + var elementsToProcess = new List(); + foreach (var item in TimeMachineHistories[0]) + { + if (item.CommitType == TimeMachineHistoryType.ElementInsert && + !item.StrokeHasBeenCleared && + item.InsertedElement != null && + (removed0 == null || !removed0.Contains(item.InsertedElement))) + { + elementsToProcess.Add(item.InsertedElement); + } + ApplyHistoryToCanvas(item, null, removed0); + } RestoreMultiTouchModeState(0); + ProcessElementsAfterRestore(elementsToProcess); } else { timeMachine.ImportTimeMachineHistory(TimeMachineHistories[CurrentWhiteboardIndex]); var removed = CollectRemovedElementsFromHistory(TimeMachineHistories[CurrentWhiteboardIndex]); - foreach (var item in TimeMachineHistories[CurrentWhiteboardIndex]) ApplyHistoryToCanvas(item, null, removed); + var elementsToProcess = new List(); + foreach (var item in TimeMachineHistories[CurrentWhiteboardIndex]) + { + if (item.CommitType == TimeMachineHistoryType.ElementInsert && + !item.StrokeHasBeenCleared && + item.InsertedElement != null && + (removed == null || !removed.Contains(item.InsertedElement))) + { + elementsToProcess.Add(item.InsertedElement); + } + ApplyHistoryToCanvas(item, null, removed); + } RestoreMultiTouchModeState(CurrentWhiteboardIndex); + ProcessElementsAfterRestore(elementsToProcess); } @@ -266,6 +291,45 @@ namespace Ink_Canvas } } + /// + /// 在恢复页面后统一处理所有图片/媒体元素的位置和事件绑定,提升含图片页面的加载性能。 + /// 先批量添加所有元素到画布,再统一处理位置和事件,减少布局更新次数。 + /// + private void ProcessElementsAfterRestore(List elements) + { + if (elements == null || elements.Count == 0) return; + + // 使用低优先级异步处理,让 UI 先响应,图片位置和事件绑定稍后完成 + Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => + { + foreach (var element in elements) + { + if (!inkCanvas.Children.Contains(element)) continue; + + if (element is Image img) + { + double left = InkCanvas.GetLeft(img); + double top = InkCanvas.GetTop(img); + if (double.IsNaN(left) || double.IsNaN(top)) + { + CenterAndScaleElement(img); + } + BindElementEvents(img); + } + else if (element is MediaElement media) + { + double left = InkCanvas.GetLeft(media); + double top = InkCanvas.GetTop(media); + if (double.IsNaN(left) || double.IsNaN(top)) + { + CenterAndScaleElement(media); + } + BindElementEvents(media); + } + } + })); + } + /// /// 恢复多指书写模式状态 /// diff --git a/Ink Canvas/MainWindow_cs/MW_TimeMachine.cs b/Ink Canvas/MainWindow_cs/MW_TimeMachine.cs index 8bb5b560..39b72d1a 100644 --- a/Ink Canvas/MainWindow_cs/MW_TimeMachine.cs +++ b/Ink Canvas/MainWindow_cs/MW_TimeMachine.cs @@ -245,38 +245,25 @@ namespace Ink_Canvas { targetCanvas.Children.Add(item.InsertedElement); - // 重新绑定事件处理器(仅对主画布) - if (targetCanvas == inkCanvas) + if (targetCanvas != inkCanvas) { if (item.InsertedElement is Image img) { - // 检查图片是否有位置信息,如果没有则应用居中 double left = InkCanvas.GetLeft(img); double top = InkCanvas.GetTop(img); - if (double.IsNaN(left) || double.IsNaN(top)) { - // 图片没有位置信息,应用居中 CenterAndScaleElement(img); } - - // 重新绑定事件处理器 - BindElementEvents(img); } else if (item.InsertedElement is MediaElement media) { - // 检查媒体元素是否有位置信息,如果没有则应用居中 double left = InkCanvas.GetLeft(media); double top = InkCanvas.GetTop(media); - if (double.IsNaN(left) || double.IsNaN(top)) { - // 媒体元素没有位置信息,应用居中 CenterAndScaleElement(media); } - - // 重新绑定事件处理器 - BindElementEvents(media); } } }