具可執行程式碼的選用套件Optional packages with executable code

具有可執行程式碼的選擇性套件適用于分割大型或複雜的應用程式,或新增至已發佈的應用程式。Optional packages with executable code are useful for dividing a large or complex app, or for adding on to an app that's already been published. 使用 Visual Studio 2017 版本 15.7 和 .NET Native 2.1,您可以從 C++ 與 C# 選用套件載入可執行程式碼。With Visual Studio 2017, version 15.7 and .NET Native 2.1, you can load executable code from both C++ and C# optional packages.

先決條件Prerequisites

  • Visual Studio 2019 或 Visual Studio 2017 (15.7 版或更新版本)Visual Studio 2019 or Visual Studio 2017 (version 15.7 or later)
  • Windows 10 1709 版或更新版本Windows 10, version 1709 or later
  • Windows 10 版本 1709 SDK 或更新版本Windows 10, version 1709 SDK or later

若要取得最新的開發工具,請參閱適用於 Windows 10 的下載項目與工具To get the latest development tools, see Downloads and tools for Windows 10.

注意

若要提交使用了選用套件和/或相關集合的 App 至 Microsoft Store,您將需要權限。To submit an app that uses optional packages and/or related sets to the Store, you will need permission. 選擇性套件和相關集合可以用於企業營運(LOB)或企業應用程式(如果未提交至存放區,則不含合作夥伴中心許可權)。Optional packages and related sets can be used for Line of Business (LOB) or enterprise apps without Partner Center permission if they are not submitted to the Store. 請參閱 Windows 開發人員支援,以取得提交使用選用套件和相關集合應用程式的權限。See Windows developer support to get permission to submit an app that uses optional packages and related sets.

注意

包含可執行程式碼的選擇性封裝必須是相關集合的一部分。Optional packages containing executable code must be part of a related set. 您將無法從選用的封裝執行程式碼,除非它是相關集合的一部分。You will not be able to execute code from an optional package unless it is part of a related set.

具可執行程式碼的 C++ 選用套件C++ Optional packages with executable code

若要從 C++ 選用套件載入程式碼,請參閱 GitHub 上的 OptionalPackageSample 存放庫。To load code from a C++ optional package, see the OptionalPackageSample repository on GitHub. OptionalPackageDLL 示範如何建立專案且其具有可從主要套件執行的程式碼。The OptionalPackageDLL shows how to create a project with code that can be executed from the main package. MyMainApp 專案示範如何從 OptionalPackageDLL.dll 檔案載入程式碼The MyMainApp project demonstrates how to load code from the OptionalPackageDLL.dll file.

具可執行程式碼的 C# 選用套件C# Optional packages with executable code

若要開始以 C# 建置選用的程式碼套件,請依照下列步驟來設定您的解決方案:To get started building an optional code package in C#, follow the below steps to configure your solution:

  1. 建立新的 UWP 應用程式且最小版本設定為 Windows 10 Fall Creators Update SDK (組建 16299) 或更高。Create a new UWP application with the minimum version set to the Windows 10 Fall Creators Update SDK (Build 16299) or higher.

  2. 新增選用的程式碼套件 (Windows 通用) 專案到解決方案。Add a new Optional Code Package (Universal Windows) project to the solution. 確保最小版本目標版本符合您的主應用程式。Ensure the Minimum Version and Target Version match that of your main app.

  3. 如果您打算提交 App 到 Microsoft Store,以滑鼠右鍵按一下兩個專案並選取** Microsoft Store -> 關聯 App 與 Microsoft Store...**If you plan to submit your apps to the Store, right click on both projects and select Store -> Associate App with the Store...

  4. 開啟主應用程式的 Package.appxmanifest 檔案並尋找 Identity Name 值。Open the Package.appxmanifest file of the main app and find the Identity Name value. 為下一個步驟記下這個值。Make a note of this value for the next step.

  5. 開啟選用應用程式套件的 Package.appxmanifest 檔案並尋找 uap3:MainAppPackageDependency Name 值。Open the optional app package's Package.appxmanifest file and find the uap3:MainAppPackageDependency Name value. 更新 uap3:MainAppPackageDependency Name 值以符合上一個步驟中的主應用程式套件的 Identity Name 值。Update the uap3:MainAppPackageDependency Name value to match the Identity Name value of the main app package from the previous step.

    以下是主應用程式的 Package.appxmanifestIdentity 範例。Here's an example of the Identity from the main app's Package.appxmanifest.

    <Identity Name="12345.MainAppProject" Publisher="CN=PublisherName" Version="1.0.0.0" />
    

    選用應用程式套件的 uap3:MainPackageDependency 必須符合主應用程式 IdentityThe optional app package's uap3:MainPackageDependency needs to be updated to match the main app's Identity.

    <uap3:MainPackageDependency Name="12345.MainAppProjectTest" />
    
  6. 新增 Bundle.mapping.txt 檔案到主應用程式中。Add a Bundle.mapping.txt file to the main app. 請依照此相關集合一節中的步驟,建立包含這兩個 App 的相關的設定。Follow the steps in this Related sets section to create a related set containing both apps.

  7. 建置選用套件專案,然後從 ..\[PathToOptionalPackageProject]\bin\[architecture]\[configuration]\Reference 找到的組建瀏覽到輸出中的套件參考資料夾。Build the optional package project and then navigate to the package Reference folder in the output from the build found at ..\[PathToOptionalPackageProject]\bin\[architecture]\[configuration]\Reference. 請注意,您可以選擇參考資料夾的路徑中的任何架構,因為 .winmd 檔案 (步驟 8) 是架構獨立。Note that you can choose any architecture in the path to the Reference folder since the .winmd file (step 8) is architecture independent.

  8. 新增主應用程式專案的參考到此資料夾中找到的 .winmd 檔案。Add a reference from the main app project to the .winmd file found in this folder. 每次您在選用套件專案中變更 API 介面區域,此 .winmd 檔案必須更新。Every time you change the API surface area in the optional package project, this .winmd file must be updated. 此參考提供主應用程式專案編譯所需的資訊。This reference provides the main app project with the necessary information to compile.

  9. 在主應用程式專案中,瀏覽至專案組建內容,並選取 [利用 .NET 原生工具鏈進行編譯]。In the main app project, navigate to the project build properties and select Compile with .NET Native tool chain. 目前,C# 的選用程式碼套件建立只支援在 .NET Native 中偵錯。Currently, only debugging in .NET Native is supported for optional code package creation in C#. 移至專案偵錯內容,並選取 [部署選用套件]。Go to the project debug properties and select Deploy optional packages. 這樣可確保每當您部署主應用程式專案時,兩個套件是同步的。This will ensure that both packages are in sync whenever you deploy the main app project.

完成這些步驟之後,您可以將程式碼加入至選擇性封裝專案,就像是 managed WinRT 元件專案一樣。After you're finished with these steps, you can add code to the optional package project as if it were a managed WinRT Component project. 若要存取主應用程式專案中的程式碼,請呼叫選用套件專案中公開的公用方法。To access the code in the main app project, call the public methods exposed in the optional package project.