在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的印刷路徑。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 44,051 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

仍在滾動?

想要快速證明嗎? PM > Install-Package IronPrint
運行範例 看看您的文件如何到達印表機。