逐步解說:使用設計工具依 ClickOnce 部署 API 的要求下載附屬組件

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

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

本逐步解說將示範如何將您的附屬組件標記為選擇項,並只為用戶端機器下載其目前的文化特性設定所需的組件。

注意事項注意事項

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

必要條件

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

若要將附屬組件標記為選擇項

  1. 建置您的專案。這個步驟將會針對您在進行當地語系化的所有文化特性產生附屬組件。

  2. 以滑鼠右鍵按一下 [方案總管] 中的專案名稱,再按一下 [屬性]。

  3. 按一下 [發行] 索引標籤,然後按一下 [應用程式檔案]。

  4. 選取 [顯示所有檔案] 核取方塊,顯示附屬組件。所有附屬組件預設都會包含在部署中,而且會顯示在這個對話方塊中。

    附屬組件名稱的格式如下:isoCode\ApplicationName.resources.dll,其中 isoCode 是採用 RFC 1766 格式的語言識別項 (ID)。

  5. 在每個語言識別項的 [下載群組] 清單內按一下 [新增]。在系統提示您輸入下載群組名稱時,請輸入語言識別項。例如,如果是日文的附屬組件,您可以將下載群組名稱指定為 ja-JP。

  6. 關閉 [應用程式檔案] 對話方塊。

若要在 C# 內視需要下載附屬組件

  1. 開啟 Program.cs 檔案。如果您沒有在 [方案總管] 內看到這個檔案,請選取您的專案,並在 [專案] 功能表上按一下 [顯示所有檔案]。

  2. 使用下列程式碼來下載適當的附屬組件,並啟動您的應用程式。

    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 this error to the user, because a satellite
                            // assembly may not exist if the user's culture and the application's
                            // default culture match.
                        }
                    }
                }
            }
    
        }
    }
    

若要在 Visual Basic 內視需要下載附屬組件

  1. 在應用程式的 [屬性] 視窗中,按一下 [應用程式] 索引標籤。

  2. 在索引標籤頁的下方,按一下 [檢視應用程式事件]。

  3. 將下列匯入內容加入 ApplicationEvents.VB 檔案的開頭。

    Imports System.Deployment.Application
    Imports System.Globalization
    Imports System.Threading
    
  4. 將下列程式碼加入 MyApplication 類別。

    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
        Thread.CurrentThread.CurrentUICulture = New CultureInfo("ja-JP")
        GetSatelliteAssemblies(Thread.CurrentThread.CurrentUICulture.ToString())
    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 this error to the user, because a satellite
                    ' assembly may not exist if the user's culture and the application's
                    ' default culture match.
                End Try
            End If
        End If
    End Sub
    

後續步驟

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

請參閱

工作

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

概念

當地語系化 ClickOnce 應用程式