共用方式為


逐步解說:使用設計工具透過 ClickOnce 部署 API 隨需下載組件

根據預設,ClickOnce 應用程式中包含的所有元件都會在第一次執行應用程式時下載。 不過,應用程式的某些部分可能由一小部分使用者使用。 在這種情況下,您只需在創建其某一類型時才下載組件。 下列逐步解說示範如何將應用程式中的特定元件標示為「選擇性」,以及如何在公共語言執行階段需要時使用命名空間中的 System.Deployment.Application 類別來下載它們。

備註

ApplicationDeployment命名空間中的System.Deployment.Application類別和 API 不支援 .NET Core 和 .NET 5 和更新版本。 在 .NET 7 中,支援存取應用程式部署屬性的新方法。 如需詳細資訊,請參閱 存取 .NET 中的 ClickOnce 部署屬性。 .NET 7 不支援對等的 ApplicationDeployment 方法。

備註

您的應用程式必須以完全信任的方式執行,才能使用此程式。

備註

您看到的對話框和功能表命令可能與說明中描述的對話框和功能表命令不同,視您的使用中設定或版本而定。 若要變更您的設定,請按一下「工具」功能表上的「匯入和匯出設定」。 如需詳細資訊,請參閱 重設設定

建立專案

若要建立使用隨選元件並搭配 Visual Studio 的專案

  1. 在 Visual Studio 中建立新的 Windows Forms 專案。 在 [ 檔案 ] 功能表上,指向 [新增],然後按一下 [新增專案]。 在對話方塊中選擇 [類別程式庫] 專案,並將其命名為 ClickOnceLibrary

    備註

    在 Visual Basic 中,建議您修改專案屬性,將此專案的根命名空間變更為 Microsoft.Samples.ClickOnceOnDemand 您選擇的命名空間。 為簡單起見,本逐步解說中的兩個專案位於相同的命名空間中。

  2. 定義一個名為 DynamicClass 的類別,其單一屬性 Message名為 。

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Microsoft.Samples.ClickOnceOnDemand
    {
        public class DynamicClass
        {
            public DynamicClass() {}
    
            public string Message
            {
                get
                {
                    return ("Hello, world!");
                }
            }
        }
    }
    
  3. [方案總管] 中選取 [Windows Forms] 專案。 新增對System.Deployment.Application程序集的參考,並對ClickOnceLibrary專案新增專案參考。

    備註

    在 Visual Basic 中,建議您修改專案屬性,將此專案的根命名空間變更為 Microsoft.Samples.ClickOnceOnDemand 您選擇的命名空間。 為簡單起見,本逐步解說中的兩個專案位於相同的命名空間中。

  4. 右鍵按表單,從功能表中選取檢視程式碼,然後將下列參考新增至表單。

    using System.Reflection;
    using System.Deployment.Application;
    using Microsoft.Samples.ClickOnceOnDemand;
    using System.Security.Permissions;
    
  5. 新增下列程式碼,以視需要下載此元件。 此程式碼示範如何使用泛型 Dictionary 類別將一組元件對應至群組名稱。 因為我們在此逐步解說中只下載單一元件,所以我們的群組中只有一個元件。 在實際應用程式中,您可能想要同時下載與應用程式中單一功能相關的所有元件。 對應資料表可讓您將屬於功能的所有 DLL 與下載群組名稱建立關聯,輕鬆執行此動作。

    // Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample,
    // but will be important in real-world applications where a feature is spread across multiple DLLs,
    // and you want to download all DLLs for that feature in one shot. 
    Dictionary<String, String> DllMapping = new Dictionary<String, String>();
    
    [SecurityPermission(SecurityAction.Demand, ControlAppDomain=true)]
    public Form1()
    {
        InitializeComponent();
    
        DllMapping["ClickOnceLibrary"] = "ClickOnceLibrary";
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }
    
    /*
     * Use ClickOnce APIs to download the assembly on demand.
     */
    private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        Assembly newAssembly = null;
    
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
    
            // Get the DLL name from the Name argument.
            string[] nameParts = args.Name.Split(',');
            string dllName = nameParts[0];
            string downloadGroupName = DllMapping[dllName];
    
            try
            {
                deploy.DownloadFileGroup(downloadGroupName);
            }
            catch (DeploymentException de)
            {
                MessageBox.Show("Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
                throw (de);
            }
    
            // Load the assembly.
            // Assembly.Load() doesn't work here, as the previous failure to load the assembly
            // is cached by the CLR. LoadFrom() is not recommended. Use LoadFile() instead.
            try
            {
                newAssembly = Assembly.LoadFile(Application.StartupPath + @"\" + dllName + ".dll");
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
        else
        {
            //Major error - not running under ClickOnce, but missing assembly. Don't know how to recover.
            throw (new Exception("Cannot load assemblies dynamically - application is not deployed using ClickOnce."));
        }
    
    
        return (newAssembly);
    }
    
  6. [檢視 ] 功能表上,按一下 [工具箱]。 將 a Button[工具箱] 拖曳到表單上。 按兩下按鈕,並將下列程式碼 Click 新增至事件處理常式。

    private void getAssemblyButton_Click(object sender, EventArgs e)
    {
        DynamicClass dc = new DynamicClass();
        MessageBox.Show("Message: " + dc.Message);
    }
    

將組件標記為選擇性

使用 Visual Studio 將 ClickOnce 應用程式中的組件標示為可選擇的

  1. 以滑鼠右鍵按一下 [方案總管 ] 中的 Windows Forms 專案,然後按一下 [屬性]。 選取 發佈 索引標籤。

  2. 按一下 應用程式檔案 按鈕。

  3. 找到 ClickOnceLibrary.dll的列表。 將 發佈狀態 下拉式方塊設定為 包含。

  4. 展開 群組 下拉式方塊,然後選取 新增。 輸入名稱 ClickOnceLibrary 作為新的群組名稱。

  5. 繼續發佈您的應用程式,如 如何:使用發佈精靈發佈 ClickOnce 應用程式中所述。

若要使用資訊清單產生和編輯工具,在 ClickOnce 應用程式中將元件標示為選擇性 — 圖形用戶端 (MageUI.exe)

  1. 建立 ClickOnce 資訊清單,如 逐步解說:手動部署 ClickOnce 應用程式中所述。

  2. 在關閉 MageUI.exe之前,選取包含部署應用程式資訊清單的索引標籤,然後在該索引標籤中選取 [檔案] 索引標籤。

  3. 在應用程式檔案清單中找到 ClickOnceLibrary.dll,並將其檔案 類型 欄設定為 。 針對 [群組 ] 欄,輸入 ClickOnceLibrary.dll

測試新組件

若要測試您的隨選組件:

  1. 啟動使用 ClickOnce 部署的應用程式。

  2. 當您的主表單出現時,請按 Button。 您應該會在訊息方塊視窗中看到一個字串,上面寫著「你好,世界!」