不支援的目標框架上的ShowPrintDialog行為
Curtis Chau
已更新: 2026年7月6日
ShowPrintDialog 和 ShowPrintDialogAsync 需要一個UI元件來顯示列印對話框。 這些方法不支援在缺乏UI功能的目標框架上,例如純粹的.NET Core或.NET Standard構建。
在不支援的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)'
Text
對話框方法需要UI組件,這在簡單的.NET Core或.NET Standard目標下是不可用的。
解決方案
為對話框設定支援UI的框架
要獲得實際的打印對話框,請針對包含UI支持的作業系統目標進行構建:
<TargetFrameworks>net462;net472;net7.0-windows;net7.0-macos;net7.0-maccatalyst;net7.0-android</TargetFrameworks>
XML
有了這些框架之一,ShowPrintDialog 和 ShowPrintDialogAsync 可以如預期地顯示對話框。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。