增强原材料入场记录视图模型,新增 SuspendEmbeddedPrintPreviewAirspace 属性以处理 WebView2 的嵌入式打印预览遮挡问题。重构相关对话框调用逻辑,确保在弹窗期间正确管理预览状态。同时,优化原材料卡片生成确认窗口的预览逻辑,提升用户体验和界面响应性。

This commit is contained in:
geht
2026-05-15 15:46:25 +08:00
parent 0c5e29044a
commit 9201c7ca15
5 changed files with 172 additions and 46 deletions

View File

@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using YY.Admin.Core.Services;
@@ -37,8 +38,12 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
private readonly Func<RawMaterialCardGeneratePlanRow, string> _previewHtmlBuilder;
private readonly IPrintDotService _printDotService;
private bool _webViewReady;
/// <summary>增大表示又有新的预览请求,旧的后台渲染结果应丢弃。</summary>
private int _previewVersion;
private bool _isRefreshingPrinters;
private bool _suppressPrinterSave;
/// <summary>Loaded 中批量赋值选中行时跳过 Setter 内的去抖预览,再由 Loaded 单次立即刷新。</summary>
private bool _suppressPreviewSchedule;
private string? _preferredPrinterNameOnLoad;
private string _printerStatus = "加载打印机中...";
@@ -78,6 +83,11 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
if (ReferenceEquals(_selectedPlanItem, value)) return;
_selectedPlanItem = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedPlanItem)));
// 仅用属性变更触发预览刷新,避免 SelectionChanged + 绑定 双重 Navigate 造成卡顿
if (!_suppressPreviewSchedule)
{
SchedulePreviewNavigate(skipDebounce: false);
}
}
}
@@ -124,15 +134,24 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
{
await PreviewWebView.EnsureCoreWebView2Async();
_webViewReady = true;
if (SelectedPlanItem == null && PlanItems.Count > 0)
try
{
SelectedPlanItem = PlanItems[0];
_suppressPreviewSchedule = true;
if (SelectedPlanItem == null && PlanItems.Count > 0)
{
SelectedPlanItem = PlanItems[0];
}
}
RenderSelectedPreview();
finally
{
_suppressPreviewSchedule = false;
}
SchedulePreviewNavigate(skipDebounce: true);
}
catch
{
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;'>模板预览加载失败</body></html>");
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;color:#eee;background:#525659;'>模板预览加载失败</body></html>");
}
}
@@ -247,6 +266,7 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
private void OnClosing(object? sender, CancelEventArgs e)
{
Interlocked.Increment(ref _previewVersion);
SavePaneRatio(GetCurrentLeftRatio());
SaveCurrentColumnWidths();
}
@@ -330,28 +350,61 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
}
}
private void PlanGrid_OnSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
/// <summary>
/// RenderToHtml 在 UI 线程会阻塞鼠标/选中反馈;移至后台线程并做短去抖合并连点。
/// </summary>
private async void SchedulePreviewNavigate(bool skipDebounce)
{
RenderSelectedPreview();
}
private void RenderSelectedPreview()
{
if (!_webViewReady) return;
if (SelectedPlanItem == null)
if (!_webViewReady)
{
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;'>请先选择左侧卡片记录</body></html>");
return;
}
var token = Interlocked.Increment(ref _previewVersion);
try
{
var html = _previewHtmlBuilder.Invoke(SelectedPlanItem);
if (!skipDebounce)
{
await Task.Delay(45).ConfigureAwait(true);
if (token != _previewVersion)
{
return;
}
}
var row = SelectedPlanItem;
if (row == null)
{
PreviewWebView.NavigateToString(
"<html><body style='font-family:Microsoft YaHei;padding:24px;color:#eee;background:#525659;'>请先选择左侧卡片记录</body></html>");
return;
}
string html;
try
{
var capturedRow = row;
html = await Task.Run(() => _previewHtmlBuilder(capturedRow)).ConfigureAwait(true);
}
catch (Exception ex)
{
html =
"<html><body style='font-family:Microsoft YaHei;padding:24px;color:#eee;background:#525659;'>模板预览失败:"
+ System.Net.WebUtility.HtmlEncode(ex.Message)
+ "</body></html>";
}
if (token != _previewVersion || !ReferenceEquals(SelectedPlanItem, row))
{
return;
}
PreviewWebView.NavigateToString(html);
}
catch
{
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;'>模板预览加载失败</body></html>");
/* 窗口关闭或调度异常时忽略 */
}
}