在macOS的IronPrint與.NET 8 (蘋果矽片)
This article was translated from English: Does it need improvement?
Translated
View the article in English
在Apple Silicon macOS上運行IronPrint的.NET 8控制台應用程式可能在建構時由於缺少本機程式庫而失敗,並且Printer.GetPrinterNamesAsync()在運行時引發錯誤。
System.PlatformNotSupportedException: System.Drawing.Common is not supported on this platform.
macOS不提供net8.0目標從未啟用macOS特定的平臺API。這兩個缺口必須填補。
解決方案
1. 安裝本機GDI+程式庫
通過Homebrew安裝libgdiplus,以便macOS可以使用Windows預設提供的GDI+支援:
brew install mono-libgdiplus
brew install mono-libgdiplus
SHELL
沒有這個程式庫,System.Drawing沒有本機後端可以調用。
2. 針對macOS框架
將您的net8.0:
<TargetFramework>net8.0-macos</TargetFramework>
<TargetFramework>net8.0-macos</TargetFramework>
XML
這告訴.NET使用macOS平臺API而不是跨平臺stub。
警告
net8.0-macos並不是預設控制台應用程式模板的一部分。 安裝macOS工作負載或從macOS應用程式模板開始以獲得預配置的支援。完整的專案文件看起來像這樣:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-macos</TargetFramework>
<Nullable>enable</Nullable>
<UseSystemDrawing>true</UseSystemDrawing>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-macos</TargetFramework>
<Nullable>enable</Nullable>
<UseSystemDrawing>true</UseSystemDrawing>
</PropertyGroup>
</Project>
XML
3. 使用相容的列印API
在macOS上,執行印刷操作時調用ShowPrintDialogAsync(),而不是依賴僅限Windows的印刷路徑。
準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布

