逐步解說:依 ClickOnce 部署 API 的要求下載附屬組件

Windows Form 應用程式可以透過附屬組件的使用來設定多種文化特性 (Culture)。「附屬組件」(Satellite Assembly) 是包含應用程式之文化特性資源的組件 (除了應用程式的預設文化特性以外)。

如同 當地語系化 ClickOnce 應用程式中所討論的內容,您可以在相同的 ClickOnce 部署中,包含多個文化特性的多個附屬組件。根據預設,ClickOnce 會將部署內所有的附屬組件下載到用戶端機器上,雖然單一用戶端可能只需要一個附屬組件。

本逐步解說將示範如何將您的附屬組件標記為選擇項,並只為用戶端機器下載其目前的文化特性設定所需的組件。下列程序會使用 Windows Software Development Kit (SDK) 內提供的工具,您也可以在 Visual Studio 內執行這項工作。如需詳細資訊,請參閱逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件.

注意事項注意事項

為了進行測試,下列程式碼範例會以程式設計方式將文化特性設定為 ja-JP。如需如何為實際執行環境調整這段程式碼的詳細資訊,請參閱本主題之後的「後續步驟」一節。

必要條件

這個主題假設您知道如何使用 Visual Studio 將當地語系化資源加入應用程式中。如需詳細資訊,請參閱逐步解說:將 Windows Form 當地語系化

若要視需要下載附屬組件

  1. 將下列程式碼加入您的應用程式中,使其能夠實現視需要下載附屬組件的功能。

    Imports System.Deployment.Application
    Imports System.Globalization
    Imports System.Threading
    
    Public Class Form1
        Shared Sub Main(ByVal args As String())
            Application.EnableVisualStyles()
    
            Thread.CurrentThread.CurrentUICulture = New CultureInfo("ja-JP")
            GetSatelliteAssemblies(Thread.CurrentThread.CurrentUICulture.ToString())
    
            Application.Run(New Form1())
        End Sub
    
        Private Shared Sub GetSatelliteAssemblies(ByVal groupName As String)
            If (ApplicationDeployment.IsNetworkDeployed) Then
    
                Dim deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    
                If (deploy.IsFirstRun) Then
                    Try
                        deploy.DownloadFileGroup(groupName)
                    Catch de As DeploymentException
                        ' Log error. Do not report error to the user, as there may not be a satellite
                        ' assembly if the user's culture and the application's default culture match.
    
                    End Try
                End If
            End If
        End Sub
    End Class
    
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    using System.Globalization;
    using System.Deployment.Application;
    using System.Reflection;
    
    namespace ClickOnce.SatelliteAssemblies
    {
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("ja-JP");
    
                // Call this before initializing the main form, which will cause the resource manager
                // to look for the appropriate satellite assembly.
                GetSatelliteAssemblies(Thread.CurrentThread.CurrentCulture.ToString());
    
                Application.Run(new Form1());
            }
    
            static void GetSatelliteAssemblies(string groupName)
            {
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
    
                    if (deploy.IsFirstRun)
                    {
                        try
                        {
                            deploy.DownloadFileGroup(groupName);
                        }
                        catch (DeploymentException de)
                        {
                            // Log error. Do not report error to the user, as there may not be a satellite
                            // assembly if the user's culture and the application's default culture match.
                        }
                    }
                }
            }
    
        }
    }
    
  2. 使用Resgen.exe (資源檔產生器) 或 Visual Studio 為您的應用程式產生附屬組件。

  3. 使用 MageUI.exe 產生應用程式資訊清單或開啟現有的應用程式資訊清單。如需這個工具的詳細資訊,請參閱 MageUI.exe (圖形用戶端、資訊清單產生和編輯工具)

  4. 按一下 [檔案] 索引標籤。

  5. 按一下 [省略符號] 按鈕 (...) 並選取包含所有應用程式組件和檔案的目錄,包括您使用 Resgen.exe 產生的附屬組件。(附屬組件的名稱格式為 isoCode\ApplicationName.resources.dll,其中 isoCode 是採用 RFC 1766 格式的語言識別項)。

  6. 按一下 [填入],將檔案加入您的部署中。

  7. 針對每個附屬組件選取 [選擇項] 核取方塊。

  8. 將每個附屬組件的群組欄位設定為其 ISO 語言識別項 (ID)。例如,如果是日文的附屬組件,您可以將下載群組名稱指定為 ja-JP。如此一來,將可讓您在步驟 1 中加入的程式碼能夠下載適當的附屬組件 (根據使用者的 CurrentUICulture 屬性設定)。

後續步驟

在實際執行環境中,您可能需要移除程式碼範例中,將 CurrentUICulture 設定為特定值的那一行,因為用戶端機器預設會設定正確的值。當您的應用程式在日文的用戶端機器上執行時,CurrentUICulture 預設將會是 ja-JP。以程式設計方式設定這個值是在部署應用程式前測試附屬組件的一個好方法。

請參閱

概念

當地語系化 ClickOnce 應用程式