如何為 .NET MAUI Android 和 iOS 開發(fā)構(gòu)建 NuGet 包

如果您嘗試將 Xamarin 移動項目移植到 .NET MAUI,您會發(fā)現(xiàn)為 Xamarin Android 構(gòu)建的包沒有問題,但為 Xamarin iOS 構(gòu)建的包不兼容。要解決此問題,您需要將目標框架更改為 .NET 6 并重建相關(guān)包桐磁。Dynamsoft 移動條碼 SDK 目前只支持 Xamarin。本文旨在幫助您使用 .NET MAUI 開發(fā)移動條碼 QR 碼掃描儀應(yīng)用程序讲岁。您將了解如何從頭開始構(gòu)建基于 Android *.aar 和 iOS 框架的 .NET 綁定庫我擂,以及如何將 Android 和 iOS SDK 打包到單個 NuGet 包中。

先決條件

下載Dynamsoft Mobile Barcode Scanner SDK

為 Android 和 iOS 創(chuàng)建 .NET 綁定庫

假設(shè)您已在 Windows 和 macOS 上安裝了 Visual Studio 2022 Preview缓艳。盡管無需安裝 Visual Studio for Mac 即可遠程與 macOS 配對校摩,但適用于 iOS 的 .NET 綁定庫的輸出包在 Windows 和 macOS 之間是不同的。我們將在以下段落中討論它阶淘。

Visual Studio 庫綁定項目

在 Visual Studio 2022 中搜索時binding衙吩,您會看到有兩種庫綁定項目。一個用于 Xamarin溪窒,另一個用于 .NET 6坤塞。要構(gòu)建與 .NET MAUI 兼容的庫,我們選擇不帶(Xamarin).

為 .NET 6 綁定 Android AAR 包

  1. 拖到DynamsoftBarcodeReader.aarAndroid 綁定項目澈蚌。
  2. 選擇DynamsoftBarcodeReader.aar并將構(gòu)建操作更改為AndroidLibrary.
  1. 將構(gòu)建模式更改為Release.

  2. 構(gòu)建項目摹芙。以下是輸出文件:

    • 安卓.dll
    • 安卓.pdb
    • 安卓.xml
    • DynamsoftBarcodeReader.aar
  3. 右鍵單擊該csproj文件并選擇Pack創(chuàng)建 NuGet 包。

為 .NET 6 綁定 iOS 框架

  1. 拖動DynamsoftBarcodeReader.xcframework到 iOS 綁定項目宛瞄。

  2. 將以下代碼添加到 csproj 文件浮禾。

    <ItemGroup>
        <NativeReference Include="DynamsoftBarcodeReader.xcframework">
        <Kind>Framework</Kind>
        <Frameworks></Frameworks>
        </NativeReference>
    </ItemGroup>
    
    
  3. 使用macOS 的命令行工具Objective Sharpie來生成ApiDefinition.csStructsAndEnums.cs文件。

  4. 構(gòu)建項目份汗。以下是輸出文件:

    • ios.dll
    • ios.pdb
    • ios.resources
      • DynamsoftBarcodeReader.xcframework
  5. 單擊Pack以創(chuàng)建 NuGet 包∮纾現(xiàn)在您將看到 Windows 和 macOS 之間的區(qū)別。Visual Studio for Windows 生成的輸出包僅包含ios.dll. 相比之下杯活,Visual Studio for macOS 生成的包包含ios.dll,manifestDynamsoftBarcodeReader.xcframework. 顯然匆帚,在 Windows 上構(gòu)建的軟件包無法運行。不過不用擔心轩猩,根據(jù) macOS 上構(gòu)建的包的結(jié)構(gòu)卷扮,我們可以在 Windows 上構(gòu)建一個可行的包荡澎。

將 Android 和 iOS SDK 打包到一個 NuGet 包中

由于我們使用 Visual Studio 構(gòu)建了 Android 和 iOS 包均践,我們可以將它們分別發(fā)布到 NuGet.org晤锹。但是,如果您想將它們構(gòu)建到單個 NuGet 包中怎么辦彤委?我們可以使用nuspec文件來實現(xiàn)這一點鞭铆。

以下是步驟:

  1. 創(chuàng)建一個nuspec文件。

    nuget spec
    
    
  2. 添加files元素并指定要包含在包中的文件列表焦影。

    <files>
        <file src="README.md" target="" />
    
        <file src="android/bin/Release/net6.0-android/android.dll" target="lib/net6.0-android31.0/" />
        <file src="android/bin/Release/net6.0-android/android.xml" target="lib/net6.0-android31.0/" />
        <file src="android/bin/Release/net6.0-android/DynamsoftBarcodeReader.aar" target="lib/net6.0-android31.0/" />
    
        <file src="ios/bin/Release/net6.0-ios/ios.dll" target="lib/net6.0-ios15.4/" />
        <file src="ios/manifest" target="lib/net6.0-ios15.4/ios.resources" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/Info.plist" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework" />
    
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Info.plist" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/DynamsoftBarcodeReader" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Headers/DynamsoftBarcodeReader.h" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Headers" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Headers/DynamsoftBarcodeSDK.h" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Headers" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Modules/module.modulemap" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64/DynamsoftBarcodeReader.framework/Modules" />
    
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Info.plist" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/DynamsoftBarcodeReader" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Headers/DynamsoftBarcodeReader.h" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Headers" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Headers/DynamsoftBarcodeSDK.h" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Headers" />
        <file src="ios/bin/Release/net6.0-ios/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Modules/module.modulemap" target="lib/net6.0-ios15.4/ios.resources/DynamsoftBarcodeReader.xcframework/ios-arm64_x86_64-simulator/DynamsoftBarcodeReader.framework/Modules" />
    </files>
    
    

    manifest用于 iOS 構(gòu)建的文件由 Visual Studio for Mac 自動生成车遂。

    <BindingAssembly>
        <NativeReference Name="DynamsoftBarcodeReader.xcframework">
            <ForceLoad></ForceLoad>
            <Frameworks></Frameworks>
            <IsCxx></IsCxx>
            <Kind>Framework</Kind>
            <LinkerFlags></LinkerFlags>
            <NeedsGccExceptionHandling></NeedsGccExceptionHandling>
            <SmartLink></SmartLink>
            <WeakFrameworks></WeakFrameworks>
        </NativeReference>
    </BindingAssembly>
    
    

    此外,為不同的目標框架添加依賴項斯辰。

    <dependencies>
      <group targetFramework="net6.0-android31.0">
      </group>
      <group targetFramework="net6.0-ios15.4">
        <dependency id="System.Runtime.InteropServices.NFloat.Internal" version="6.0.1" exclude="Build,Analyzers" />
      </group>
    </dependencies>
    
    
  3. 構(gòu)建包:

    nuget pack
    
    

現(xiàn)在您可以為 .NET MAUI 移動開發(fā)安裝Barcode.NET.Mobile 舶担。

試試 .NET MAUI 條碼二維碼掃描器

https://github.com/yushulx/maui-barcode-qrcode-scanner

源代碼

https://github.com/yushulx/barcode-dotnet-mobile

來源:https://dev.to/yushulx/how-to-build-a-nuget-package-for-net-maui-android-and-ios-development-37pl

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市彬呻,隨后出現(xiàn)的幾起案子衣陶,更是在濱河造成了極大的恐慌,老刑警劉巖闸氮,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件剪况,死亡現(xiàn)場離奇詭異,居然都是意外死亡蒲跨,警方通過查閱死者的電腦和手機译断,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來或悲,“玉大人孙咪,你說我怎么就攤上這事⊙灿铮” “怎么了翎蹈?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長捌臊。 經(jīng)常有香客問我杨蛋,道長,這世上最難降的妖魔是什么理澎? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任逞力,我火速辦了婚禮,結(jié)果婚禮上糠爬,老公的妹妹穿的比我還像新娘寇荧。我一直安慰自己,他們只是感情好执隧,可當我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布揩抡。 她就那樣靜靜地躺著户侥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪峦嗤。 梳的紋絲不亂的頭發(fā)上蕊唐,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機與錄音烁设,去河邊找鬼替梨。 笑死,一個胖子當著我的面吹牛装黑,可吹牛的內(nèi)容都是我干的副瀑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恋谭,長吁一口氣:“原來是場噩夢啊……” “哼糠睡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起疚颊,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤狈孔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后串稀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體除抛,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年母截,在試婚紗的時候發(fā)現(xiàn)自己被綠了到忽。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡清寇,死狀恐怖喘漏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情华烟,我是刑警寧澤翩迈,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站盔夜,受9級特大地震影響负饲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜喂链,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一返十、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧椭微,春花似錦洞坑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽刽沾。三九已至,卻和暖如春排拷,著一層夾襖步出監(jiān)牢的瞬間侧漓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工攻泼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留火架,地道東北人鉴象。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓忙菠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親纺弊。 傳聞我的和親對象是個殘疾皇子牛欢,可洞房花燭夜當晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容