İngilizce dilinde oku

Aracılığıyla paylaş


IEventBindingService Arabirim

Tanım

Bileşen olayları için olay işleyicilerini kaydetmeye yönelik bir hizmet sağlar.

C#
public interface IEventBindingService
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IEventBindingService
Türetilmiş
Öznitelikler

Örnekler

Aşağıdaki örnekte, tasarımcının IEventBindingService bileşen için eklediği özel kısayol menü komutu çağrıldığında bir bileşenin olayını tasarım zamanında bir olay işleyicisine bağlamak için kullanan bir tasarımcı gösterilmektedir. Örneği kullanmak için sınıf kitaplığına derleyin, bir Windows Forms projesinden başvuru ekleyin, Araç Kutusu'na sağ tıklayıp Araç Kutusunu Özelleştir'i seçip sınıf kitaplığını seçip Tamam'a tıklayarak sınıf kitaplığındaki bileşeni Araç Kutusu'na ekleyin ve Forma EventControl örneğini ekleyin. Ardından EventControl'e sağ tıklayın ve Connect testEvent kısayol menüsü komutuna tıklayın. Boş bir olay işleyici yöntemi oluşturulur ve EventControl'ün testEvent yöntemi Form için başlatma kodunda bu olay işleyicisine başlatılır.

C#
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace EventDesignerTest
{
    // This designer provides a "Connect testEvent" designer verb shortcut 
    // menu command. When invoked, the command attaches a new event-handler 
    // method named "testEventHandler" to the "testEvent" event of an 
    // associated control.
    // If a "testEvent" event of the associated control does not exist, 
    // the IEventBindingService declares it.
    public class EventDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public EventDesigner()
        {
        }
        
        // When the "Connect testEvent" designer verb shortcut menu 
        // command is invoked, this method uses the 
        // IEventBindingService to attach an event handler to a 
        // "textEvent" event of the associated control.
        private void ConnectEvent(object sender, EventArgs e)
        {
            IEventBindingService eventservice = (IEventBindingService)this.Component.Site.GetService(typeof(System.ComponentModel.Design.IEventBindingService));
            if( eventservice != null )
            {
                // Attempt to obtain a PropertyDescriptor for a 
                // component event named "testEvent".
                EventDescriptorCollection edc = TypeDescriptor.GetEvents(this.Component);				
                if( edc == null || edc.Count == 0 )
                    return;
                                
                EventDescriptor ed = null;
                // Search for an event named "testEvent".
                foreach(EventDescriptor edi in edc)
                    if(edi.Name == "testEvent")
                    {
                        ed = edi;
                        break;
                    }
                if( ed == null )
                    return;

                // Use the IEventBindingService to get a 
                // PropertyDescriptor for the event.
                PropertyDescriptor pd = eventservice.GetEventProperty(ed);
                if( pd == null )
                    return;				
                
                // Set the value of the event to "testEventHandler".
                pd.SetValue(this.Component, "testEventHandler");
            }
        }

                // Provides a designer verb command for the designer's 
                // shortcut menu.
        public override System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection dvc = new DesignerVerbCollection();
                dvc.Add(new DesignerVerb("Connect testEvent", new EventHandler(ConnectEvent)));
                return dvc;
            }
        }
    }

    // EventControl is associated with the EventDesigner and displays 
    // instructions for demonstrating the service.
    [Designer(typeof(EventDesigner))]
    public class EventControl : System.Windows.Forms.UserControl
    {
        public event System.EventHandler testEvent;

        public EventControl()
        {		
            this.BackColor = Color.White;	
            this.Size = new Size(320, 96);
        }

        protected override void Dispose( bool disposing )
        {
            base.Dispose( disposing );
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawString("IEventBindingService Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5);
            
            e.Graphics.DrawString("Use the \"Connect testEvent\" command of the", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 22);
            e.Graphics.DrawString("right-click shortcut menu provided by this", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 32);
            e.Graphics.DrawString("control's associated EventDesigner to create", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 42);
            e.Graphics.DrawString("a new event handler linked with the testEvent", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 52);
            e.Graphics.DrawString("of this control in the initialization code", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 62);
            e.Graphics.DrawString("for this control.", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 5, 72);
        }
    }
}

Açıklamalar

Olay bağlama hizmeti, bir olay işleyicisini tasarımcı kodundan bir bileşen olayına bağlamak için bir yol sağlar.

kullanarak IEventBindingServicebir olay işleyicisini bir bileşen olayına bağlamak için, önce bağlamak istediğiniz bileşenin olayı için bir EventDescriptor almanız gerekir. , IEventBindingService olayını bir EventDescriptor olay işleyicisi yöntemi adıyla yapılandırmak için kullanabileceğiniz bir PropertyDescriptor öğesine dönüştürebilen yöntemler sağlar.

nesnesi, TypeDescriptor bir GetEvents bileşenin her olayı için bir içeren EventDescriptor nesneler elde EventDescriptorCollection etmek için kullanabileceğiniz bir yöntem sağlar. ve GetEventPropertyGetEventProperties yöntemleri, iki yöntemden IEventBindingService birine geçirilen her EventDescriptor biri için bir PropertyDescriptor döndürür. veya'dan GetEventPropertyGetEventProperties döndürülen her PropertyDescriptor biri bir dizenin özellik türüne sahiptir. Bu dizeyi, yöntemini kullanarak olayı ile bağlamak için olay işleyicisi yönteminin adını belirten bir değere SetValuePropertyDescriptorayarlayabilirsiniz.

Yöntemler

CreateUniqueMethodName(IComponent, EventDescriptor)

Belirtilen bileşen ve olay için bir olay işleyici yöntemi için benzersiz bir ad oluşturur.

GetCompatibleMethods(EventDescriptor)

Belirtilen olayla uyumlu bir yöntem imzası olan olay işleyicisi yöntemlerinin koleksiyonunu alır.

GetEvent(PropertyDescriptor)

Belirtilen özellik tanımlayıcısının bir EventDescriptor olayı temsil ediyorsa temsil eden olay için bir alır.

GetEventProperties(EventDescriptorCollection)

Bir olay tanımlayıcıları kümesini bir özellik tanımlayıcıları kümesine dönüştürür.

GetEventProperty(EventDescriptor)

Tek bir olay tanımlayıcısını özellik tanımlayıcısına dönüştürür.

ShowCode()

Tasarımcının kullanıcı kodunu görüntüler.

ShowCode(IComponent, EventDescriptor)

Belirtilen olayın kullanıcı kodunu görüntüler.

ShowCode(Int32)

Tasarımcının kullanıcı kodunu belirtilen satırda görüntüler.

Şunlara uygulanır

Ürün Sürümler
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Ayrıca bkz.