مشاركة عبر


الإرشادات التفصيلية: تحميل التجميعات عند الطلب مع توزيع ClickOnce API باستخدام "مصمم"

افتراضياً، يكون لكافة تجميعات في ClickOnceيتم تحميل تطبيق عند تطبيق هو التشغيل أول. ومع ذلك، قد تكون هناك أجزاء تطبيق الخاص بك التي تستخدم بواسطة التعيين صغيرة من المستخدمين. في هذه الحالة، تحتاج إلى ينزّل تجميع فقط عندما تقوم بإنشاء واحد الأنواع الخاصة به. معاينة التالية يوضح كيفية وضع علامة على بعض تجميعات في تطبيق الخاص بك ك "اختياري"، وكيف ينزّل بها باستخدام الفئات في System.Deployment.Applicationمساحة الاسم عند وقت تشغيل اللغة العامة إلزامية لهم.

ملاحظة

سيكون للتطبيق الخاص بك إلى تشغيله في وضع الثقة الكاملة إلى استخدام هذا إجراء.

ملاحظة

قد تختلف مربعات الحوار وأوامر القائمة التى تشاهدها الان عن تلك الموصوفة في التعليمات اعتماداً على الإعدادات النشطة أو الإصدار الخاص بك. لتغيير الإعدادات اضغط Import and Export إعدادات ضمن أدواتالقائمة. لمزيد من المعلومات، راجع العمل مع إعدادات.

إنشاء المشروع

إلى إنشاء مشروع يستخدم تجميع حسب الطلب باستخدام ‏‫Visual Studio

  1. قم بإنشاء مشروع جديد لنماذج Windows في Visual Studio. في قائمة ملف ، أشر إلى إضافة، و ثم انقر فوق مشروع جديد. قم باختيار مشروع مكتبة فئة في صندوق الحوار و تسمية من ClickOnceLibrary.

    ملاحظة

    في Visual أساسى، من المستحسن أن قمت بتعديل خصائص مشروع لتغيير مساحة الاسم الجذر لهذا مشروع إلى Microsoft.Samples.ClickOnceOnDemandأو لمساحة اسم الخاص بك الاختيار. للبساطة، الثاني مشاريع في هذه معاينة الموجودة في نفس مساحة الاسم.

  2. تعريف فئة باسم DynamicClassمع مفرد خاصية المسماة Message.

    Public Class DynamicClass
        Sub New()
    
        End Sub
    
        Public ReadOnly Property Message() As String
            Get
                Message = "Hello, world!"
            End Get
        End Property
    End Class
    
    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 أساسى، من المستحسن أن قمت بتعديل خصائص مشروع لتغيير مساحة الاسم الجذر لهذا مشروع إلى Microsoft.Samples.ClickOnceOnDemandأو لمساحة اسم الخاص بك الاختيار. للبساطة، بين مشاريع في هذه معاينة الموجودة في نفس مساحة الاسم.

  4. انقر نقراً مزدوجاً فوق النموذج، ثم انقر فوق عرض تعليمات برمجية من قائمة، و إضافة المراجع التالية إلى النموذج.

    Imports System.Reflection
    Imports System.Deployment.Application
    Imports System.Collections.Generic
    Imports Microsoft.Samples.ClickOnceOnDemand
    Imports System.Security.Permissions
    
    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. 
    Dim DllMappingTable As New Dictionary(Of String, String)()
    
    <SecurityPermission(SecurityAction.Demand, ControlAppDomain:=True)> _
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        DllMappingTable("ClickOnceLibrary") = "ClickOnceLibrary"
    End Sub
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf Me.CurrentDomain_AssemblyResolve
    End Sub
    
    Private Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly
        Dim NewAssembly As Assembly = Nothing
    
        If (ApplicationDeployment.IsNetworkDeployed) Then
            Dim Deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    
            ' Get the DLL name from the argument.
            Dim NameParts As String() = args.Name.Split(",")
            Dim DllName As String = NameParts(0)
            Dim DownloadGroupName As String = DllMappingTable(DllName)
    
            Try
                Deploy.DownloadFileGroup(DownloadGroupName)
            Catch ex As Exception
                MessageBox.Show("Could not download file group from Web server. Contact administrator. Group name: " & DownloadGroupName & "; DLL name: " & args.Name)
                Throw (ex)
            End Try
    
            ' 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 ex As Exception
                Throw (ex)
            End Try
        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.")
        End If
    
        Return NewAssembly
    End Function
    
    // 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. في القائمة عرض، انقر فوق تفاصيل. بسحب Buttonمن مربع أدوات التحكم إلى النموذج. انقر نقراً مزدوجاً فوق زر ثم قم بإضافة التعليمة البرمجية التالية إلى Clickمعالج حدث.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DC As New DynamicClass()
        MessageBox.Show("Message is " & DC.Message)
    End Sub
    
    private void getAssemblyButton_Click(object sender, EventArgs e)
    {
        DynamicClass dc = new DynamicClass();
        MessageBox.Show("Message: " + dc.Message);
    }
    

وضع علامة كـ semblies كـ اختياري

لوضع علامة على تجميعات اختيارية في تطبيقات ClickOnce الخاص بك باستخدام برنامج ‏‫Visual Studio

  1. انقر نقراً مزدوجاً فوق مشروع Windows Forms في من مستكشف الحل ، انقر فوق خصائص. تحديد علامة تبويب ينشر.

  2. انقر فوق الزر ملفات تطبيق.

  3. بحث the listing for ClickOnceLibrary.dll. التعيين the ينشر حالة إسقاط-أسفل صندوق إلى تضمين.

  4. توسيع the مجموعة إسقاط-أسفل صندوق و تحديد جديد. Enter الزر the اسم ClickOnceLibrary كـ the مجموعة جديدة اسم.

  5. متابعة النشر your تطبيق كـ described في كيفية: نشر التطبيقات ClickOnce باستخدام "معالج النشر".

إلى mark تجميعات كـ اختياري في your ClickOnce تطبيق بواسطة using بيان Generation و ‏‏تحرير أداة — Graphical العميل (MageUI.exe)

  1. إنشاء your ClickOnce بيانات كـ described في الإرشادات التفصيلية: نشر تطبيقات ClickOnce يدوياً.

  2. قبل closing MageUI.exe, تحديد the علامة تبويب that يحتوي على your توزيع's بيان تطبيق, و within that علامة تبويب تحديد the ملفات علامة تبويب.

  3. بحث ClickOnceLibrary.dll في قائمة تطبيقات ملف s وبه عمود ملف نوع بلا . بالنسبة للأعمدة المجموعة ، اكتب من ClickOnceLibrary.dll.

اختبار تجميع جديد

إلى اختبار تجميع حسب الطلب الخاص بك

  1. يبدأ تطبيق الخاص بك ونشرها مع ClickOnce.

  2. عند ظهور نموذج رئيسي، اضغط Button. يجب أن تشاهد سلسلة في نافذة صندوق رسالة الذي يقرأ "مرحبا, World!"

راجع أيضًا:

المرجع

ApplicationDeployment