增强原材料入场记录视图模型,新增 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

@@ -114,6 +114,46 @@ public class RawMaterialEntryEditDialogViewModel : BaseViewModel, IDialogResulta
public ObservableCollection<KeyValuePair<string, string>> IsSpecialAdoptionOptions { get; } = new(); public ObservableCollection<KeyValuePair<string, string>> IsSpecialAdoptionOptions { get; } = new();
public ObservableCollection<KeyValuePair<string, string>> StatusOptions { get; } = new(); public ObservableCollection<KeyValuePair<string, string>> StatusOptions { get; } = new();
public ObservableCollection<RawMaterialSplitDetailItem> SplitCodeDetails { get; } = new(); public ObservableCollection<RawMaterialSplitDetailItem> SplitCodeDetails { get; } = new();
private bool _suspendEmbeddedPrintPreviewAirspace;
/// <summary>
/// WebView2 使用独立 HWNDAirspace会同窗体内浮在 WPF 元素之上,遮挡 HandyControl 内嵌 Dialog。
/// 原料入场独立页在弹窗期间通过绑定收起预览宿主;其它页面无 WebView2 时可忽略该属性。
/// </summary>
public bool SuspendEmbeddedPrintPreviewAirspace
{
get => _suspendEmbeddedPrintPreviewAirspace;
private set
{
if (!SetProperty(ref _suspendEmbeddedPrintPreviewAirspace, value))
{
return;
}
OnSuspendEmbeddedPrintPreviewAirspaceChanged();
}
}
/// <summary>子类在预览区可见性依赖此标志时,覆写以联动通知。</summary>
protected virtual void OnSuspendEmbeddedPrintPreviewAirspaceChanged()
{
}
/// <summary>在异步弹窗期间挂起右侧嵌入 WebView2避免遮挡模态内容。</summary>
protected async Task<TResult> SuspendEmbeddedPrintPreviewAirspaceWhileAsync<TResult>(Func<Task<TResult>> action)
{
SuspendEmbeddedPrintPreviewAirspace = true;
try
{
return await action();
}
finally
{
SuspendEmbeddedPrintPreviewAirspace = false;
}
}
public double SplitCodeTableHeight => CalculateSplitCodeTableHeight(); public double SplitCodeTableHeight => CalculateSplitCodeTableHeight();
public string SplitTotalPortionsDisplay => JoinSplitValue(item => item.Portions?.ToString(CultureInfo.InvariantCulture), true); public string SplitTotalPortionsDisplay => JoinSplitValue(item => item.Portions?.ToString(CultureInfo.InvariantCulture), true);
public string SplitPortionWeightDisplay => JoinSplitValue(item => FormatNullableDecimal(item.PortionWeight), true); public string SplitPortionWeightDisplay => JoinSplitValue(item => FormatNullableDecimal(item.PortionWeight), true);
@@ -439,13 +479,14 @@ public class RawMaterialEntryEditDialogViewModel : BaseViewModel, IDialogResulta
bool confirmed; bool confirmed;
try try
{ {
confirmed = await HandyControl.Controls.Dialog.Show<WeightRecordPickerDialogView>() confirmed = await SuspendEmbeddedPrintPreviewAirspaceWhileAsync(() =>
.Initialize<WeightRecordPickerDialogViewModel>(vm => HandyControl.Controls.Dialog.Show<WeightRecordPickerDialogView>()
{ .Initialize<WeightRecordPickerDialogViewModel>(vm =>
pickerVm = vm; {
vm.Initialize(Entry?.BillNo); pickerVm = vm;
}) vm.Initialize(Entry?.BillNo);
.GetResultAsync<bool>(); })
.GetResultAsync<bool>());
} }
catch catch
{ {
@@ -490,13 +531,14 @@ public class RawMaterialEntryEditDialogViewModel : BaseViewModel, IDialogResulta
bool confirmed; bool confirmed;
try try
{ {
confirmed = await HandyControl.Controls.Dialog.Show<WarehouseAreaPickerDialogView>() confirmed = await SuspendEmbeddedPrintPreviewAirspaceWhileAsync(() =>
.Initialize<WarehouseAreaPickerDialogViewModel>(vm => HandyControl.Controls.Dialog.Show<WarehouseAreaPickerDialogView>()
{ .Initialize<WarehouseAreaPickerDialogViewModel>(vm =>
pickerVm = vm; {
vm.Initialize(row.WarehouseLocation); pickerVm = vm;
}) vm.Initialize(row.WarehouseLocation);
.GetResultAsync<bool>(); })
.GetResultAsync<bool>());
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -521,13 +563,14 @@ public class RawMaterialEntryEditDialogViewModel : BaseViewModel, IDialogResulta
bool confirmed; bool confirmed;
try try
{ {
confirmed = await HandyControl.Controls.Dialog.Show<RawMaterialEntryMaterialPickerDialogView>() confirmed = await SuspendEmbeddedPrintPreviewAirspaceWhileAsync(() =>
.Initialize<RawMaterialEntryMaterialPickerDialogViewModel>(vm => HandyControl.Controls.Dialog.Show<RawMaterialEntryMaterialPickerDialogView>()
{ .Initialize<RawMaterialEntryMaterialPickerDialogViewModel>(vm =>
pickerVm = vm; {
vm.Initialize(Entry?.MaterialCode, Entry?.MaterialName); pickerVm = vm;
}) vm.Initialize(Entry?.MaterialCode, Entry?.MaterialName);
.GetResultAsync<bool>(); })
.GetResultAsync<bool>());
} }
catch catch
{ {
@@ -582,9 +625,10 @@ public class RawMaterialEntryEditDialogViewModel : BaseViewModel, IDialogResulta
bool confirmed; bool confirmed;
try try
{ {
confirmed = await HandyControl.Controls.Dialog.Show<SupplierPickerDialogView>() confirmed = await SuspendEmbeddedPrintPreviewAirspaceWhileAsync(() =>
.Initialize<SupplierPickerDialogViewModel>(vm => pickerVm = vm) HandyControl.Controls.Dialog.Show<SupplierPickerDialogView>()
.GetResultAsync<bool>(); .Initialize<SupplierPickerDialogViewModel>(vm => pickerVm = vm)
.GetResultAsync<bool>());
} }
catch { return; } catch { return; }

View File

@@ -170,9 +170,18 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
{ {
if (!SetProperty(ref _isPrintPreviewExpanded, value)) return; if (!SetProperty(ref _isPrintPreviewExpanded, value)) return;
if (value) _lastPreviewSnapshot = string.Empty; if (value) _lastPreviewSnapshot = string.Empty;
RaisePropertyChanged(nameof(IsPrintPreviewWebAreaVisible));
} }
} }
/// <summary>右侧下方预览区是否显示 WebView2 宿主HandyControl 内嵌弹窗期间因 Airspace 临时隐藏)。</summary>
public bool IsPrintPreviewWebAreaVisible => !SuspendEmbeddedPrintPreviewAirspace && IsPrintPreviewExpanded;
protected override void OnSuspendEmbeddedPrintPreviewAirspaceChanged()
{
RaisePropertyChanged(nameof(IsPrintPreviewWebAreaVisible));
}
private string _printPreviewStatus = string.Empty; private string _printPreviewStatus = string.Empty;
/// <summary>预览区状态提示(如离线、加载中、错误摘要)。</summary> /// <summary>预览区状态提示(如离线、加载中、错误摘要)。</summary>
public string PrintPreviewStatus public string PrintPreviewStatus
@@ -744,7 +753,9 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
Owner = Application.Current.MainWindow, Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner WindowStartupLocation = WindowStartupLocation.CenterOwner
}; };
var confirmed = confirmWindow.ShowDialog() == true; // WebView2 Airspace模态子窗体仍可能与本窗体内 HWND 叠层异常,弹窗期间收起嵌入预览
var confirmed = await SuspendEmbeddedPrintPreviewAirspaceWhileAsync(() =>
Task.FromResult(confirmWindow.ShowDialog() == true));
if (!confirmed) return; if (!confirmed) return;
var selectedPrinterName = confirmWindow.SelectedPrinterName; var selectedPrinterName = confirmWindow.SelectedPrinterName;
if (string.IsNullOrWhiteSpace(selectedPrinterName)) if (string.IsNullOrWhiteSpace(selectedPrinterName))

View File

@@ -70,6 +70,8 @@
ItemsSource="{Binding PlanItems}" ItemsSource="{Binding PlanItems}"
SelectedItem="{Binding SelectedPlanItem, Mode=TwoWay}" SelectedItem="{Binding SelectedPlanItem, Mode=TwoWay}"
AutoGenerateColumns="False" AutoGenerateColumns="False"
EnableRowVirtualization="False"
EnableColumnVirtualization="False"
HeadersVisibility="Column" HeadersVisibility="Column"
CanUserResizeColumns="True" CanUserResizeColumns="True"
CanUserReorderColumns="False" CanUserReorderColumns="False"
@@ -85,10 +87,10 @@
SelectionMode="Single" SelectionMode="Single"
SelectionUnit="FullRow" SelectionUnit="FullRow"
ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
SelectionChanged="PlanGrid_OnSelectionChanged">
<DataGrid.Resources> <DataGrid.Resources>
<Style x:Key="CenteredCellTextStyle" TargetType="TextBlock"> <Style x:Key="CenteredCellTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="#262626"/>
<Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/> <Setter Property="TextAlignment" Value="Center"/>
@@ -104,13 +106,21 @@
<Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/>
</Style> </Style>
</DataGrid.ColumnHeaderStyle> </DataGrid.ColumnHeaderStyle>
<!-- 勿覆盖默认选中配色:显式写出 IsSelected/悬停背景 + 深色文字,避免出现白字白底、“整表发白”的假死感 -->
<DataGrid.RowStyle> <DataGrid.RowStyle>
<Style TargetType="DataGridRow"> <Style TargetType="DataGridRow">
<Setter Property="Margin" Value="0,0,0,3"/> <Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="#FFFFFF"/> <Setter Property="Background" Value="#FFFFFF"/>
<Setter Property="SnapsToDevicePixels" Value="True"/> <Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#F5F5F5"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#E6F7FF"/>
</Trigger>
</Style.Triggers>
</Style> </Style>
</DataGrid.RowStyle> </DataGrid.RowStyle>
<DataGrid.CellStyle> <DataGrid.CellStyle>
@@ -119,10 +129,17 @@
<Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="8,10"/> <Setter Property="Padding" Value="8,10"/>
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="#262626"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextBlock.TextAlignment" Value="Center"/> <Setter Property="TextBlock.TextAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="#1F1F1F"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style> </Style>
</DataGrid.CellStyle> </DataGrid.CellStyle>
<DataGrid.Columns> <DataGrid.Columns>
@@ -159,7 +176,8 @@
<TextBlock Text="打印模板预览" FontSize="14" FontWeight="SemiBold"/> <TextBlock Text="打印模板预览" FontSize="14" FontWeight="SemiBold"/>
<TextBlock Text="{Binding TemplateText}" Margin="0,4,0,0" FontSize="12" Foreground="#8C8C8C"/> <TextBlock Text="{Binding TemplateText}" Margin="0,4,0,0" FontSize="12" Foreground="#8C8C8C"/>
</StackPanel> </StackPanel>
<wv2:WebView2 x:Name="PreviewWebView" Grid.Row="1" DefaultBackgroundColor="#FFFFFFFF"/> <!-- 与入场页预览区一致:加载 NavigateToString 间隙减少刺眼的整片白闪烁 -->
<wv2:WebView2 x:Name="PreviewWebView" Grid.Row="1" DefaultBackgroundColor="#FF525659"/>
</Grid> </Grid>
</Border> </Border>
</Grid> </Grid>

View File

@@ -6,6 +6,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls.Primitives; using System.Windows.Controls.Primitives;
using YY.Admin.Core.Services; using YY.Admin.Core.Services;
@@ -37,8 +38,12 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
private readonly Func<RawMaterialCardGeneratePlanRow, string> _previewHtmlBuilder; private readonly Func<RawMaterialCardGeneratePlanRow, string> _previewHtmlBuilder;
private readonly IPrintDotService _printDotService; private readonly IPrintDotService _printDotService;
private bool _webViewReady; private bool _webViewReady;
/// <summary>增大表示又有新的预览请求,旧的后台渲染结果应丢弃。</summary>
private int _previewVersion;
private bool _isRefreshingPrinters; private bool _isRefreshingPrinters;
private bool _suppressPrinterSave; private bool _suppressPrinterSave;
/// <summary>Loaded 中批量赋值选中行时跳过 Setter 内的去抖预览,再由 Loaded 单次立即刷新。</summary>
private bool _suppressPreviewSchedule;
private string? _preferredPrinterNameOnLoad; private string? _preferredPrinterNameOnLoad;
private string _printerStatus = "加载打印机中..."; private string _printerStatus = "加载打印机中...";
@@ -78,6 +83,11 @@ public partial class RawMaterialCardGenerateConfirmWindow : HandyControl.Control
if (ReferenceEquals(_selectedPlanItem, value)) return; if (ReferenceEquals(_selectedPlanItem, value)) return;
_selectedPlanItem = value; _selectedPlanItem = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedPlanItem))); 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(); await PreviewWebView.EnsureCoreWebView2Async();
_webViewReady = true; _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 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) private void OnClosing(object? sender, CancelEventArgs e)
{ {
Interlocked.Increment(ref _previewVersion);
SavePaneRatio(GetCurrentLeftRatio()); SavePaneRatio(GetCurrentLeftRatio());
SaveCurrentColumnWidths(); 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(); if (!_webViewReady)
}
private void RenderSelectedPreview()
{
if (!_webViewReady) return;
if (SelectedPlanItem == null)
{ {
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;'>请先选择左侧卡片记录</body></html>");
return; return;
} }
var token = Interlocked.Increment(ref _previewVersion);
try 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); PreviewWebView.NavigateToString(html);
} }
catch catch
{ {
PreviewWebView.NavigateToString("<html><body style='font-family:Microsoft YaHei;padding:24px;'>模板预览加载失败</body></html>"); /* 窗口关闭或调度异常时忽略 */
} }
} }

View File

@@ -1136,7 +1136,7 @@
MinHeight="200" MinHeight="200"
MaxHeight="420" MaxHeight="420"
Background="#525659" Background="#525659"
Visibility="{Binding IsPrintPreviewExpanded, Converter={StaticResource Boolean2VisibilityConverter}}"> Visibility="{Binding IsPrintPreviewWebAreaVisible, Converter={StaticResource Boolean2VisibilityConverter}}">
<wv2:WebView2 x:Name="PrintPreviewWebView" <wv2:WebView2 x:Name="PrintPreviewWebView"
DefaultBackgroundColor="#FF525659"/> DefaultBackgroundColor="#FF525659"/>
</Border> </Border>