IronPrint中的ShowPrintDialog行為(2024.9後)
This article was translated from English: Does it need improvement?
Translated
View the article in English
ShowPrintDialog 和 ShowPrintDialogAsync 在針對不支援的目標框架調用時不再拋出異常。 自版本2024.9起, 它們會記錄警告並改用PrintAsync()。
在此更改之前,調用任一方法而無相容的TargetFramework會引發以下錯誤:
System.AggregateException: 'One or more errors occurred.
(Please try Print/PrintAsync since ShowPrintDialog/ShowPrintDialogAsync in NET Core or NET Standard,
it required UI component so please set more specific OS TargetFrameworks
e.g. net462; net472; net7.0-windows; net7.0-macos; net7.0-maccatalyst; net7.0-android)'
對話框方法需要UI元件,而在純粹的.NET Core或.NET Standard目標下無法使用。 早期的版本將此視為嚴重錯誤並拋出異常。 異常在運行時具有干擾性,因此在2024.9中故意更改了行為。
解決方案
回退是自動的,但透過選擇正確的目標,您可以控制哪個路徑運行。
1. 依賴自動回退
在不支援的框架上,現在的調用會記錄警告並轉向PrintAsync()而不是拋出異常。 現有的程式碼可以繼續運行而不會崩潰,儘管未顯示列印對話框。
警告回退列印不會顯示對話框。 使用者在不支援的目標上看不到列印提示,因此除非您針對UI框架,否則不要依賴顯示對話框。
2. 針對具有UI能力的框架進行對話框
要獲得實際的列印對話框,請構建針對包括UI支持的操作系統特定目標:
<TargetFrameworks>net462;net472;net7.0-windows;net7.0-macos;net7.0-maccatalyst;net7.0-android</TargetFrameworks>
<TargetFrameworks>net462;net472;net7.0-windows;net7.0-macos;net7.0-maccatalyst;net7.0-android</TargetFrameworks>
XML
具有這些框架之一,ShowPrintDialog 和 ShowPrintDialogAsync 按預期顯示對話框。
準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布

