ServiceContainer.AddService 方法

定義

將指定的服務加入至服務容器。Adds the specified service to the service container.

多載

AddService(Type, ServiceCreatorCallback)

將指定的服務加入至服務容器。Adds the specified service to the service container.

AddService(Type, Object)

將指定的服務加入至服務容器。Adds the specified service to the service container.

AddService(Type, ServiceCreatorCallback, Boolean)

將指定的服務加入至服務容器。Adds the specified service to the service container.

AddService(Type, Object, Boolean)

將指定的服務加入至服務容器。Adds the specified service to the service container.

AddService(Type, ServiceCreatorCallback)

將指定的服務加入至服務容器。Adds the specified service to the service container.

public:
 virtual void AddService(Type ^ serviceType, System::ComponentModel::Design::ServiceCreatorCallback ^ callback);
public void AddService (Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback);
abstract member AddService : Type * System.ComponentModel.Design.ServiceCreatorCallback -> unit
override this.AddService : Type * System.ComponentModel.Design.ServiceCreatorCallback -> unit
Public Sub AddService (serviceType As Type, callback As ServiceCreatorCallback)

參數

serviceType
Type

要加入之服務的類型。The type of service to add.

callback
ServiceCreatorCallback

可以建立服務的回呼物件。A callback object that can create the service. 這允許將服務宣告為可用的,但是會將物件的建立延遲至要求服務的時候。This allows a service to be declared as available, but delays creation of the object until the service is requested.

實作

例外狀況

serviceTypecallbacknullserviceType or callback is null.

此容器內已含有 serviceType 類型的服務。A service of type serviceType already exists in the container.

範例

下列範例說明如何將服務新增至 ServiceContainerThe following example illustrates how to add a service to a ServiceContainer.

m_MyServiceContainer->AddService( Control::typeid, gcnew ServiceCreatorCallback( this, &Form1::CreateNewControl ) );
m_MyServiceContainer.AddService(typeof(Control),
                new ServiceCreatorCallback(this.CreateNewControl));
m_MyServiceContainer.AddService(GetType(Control), New ServiceCreatorCallback( _
          AddressOf CreateNewControl))

另請參閱

AddService(Type, Object)

將指定的服務加入至服務容器。Adds the specified service to the service container.

public:
 virtual void AddService(Type ^ serviceType, System::Object ^ serviceInstance);
public void AddService (Type serviceType, object serviceInstance);
abstract member AddService : Type * obj -> unit
override this.AddService : Type * obj -> unit
Public Sub AddService (serviceType As Type, serviceInstance As Object)

參數

serviceType
Type

要加入之服務的類型。The type of service to add.

serviceInstance
Object

要加入的服務執行個體。An instance of the service to add. 這個物件必須實作或繼承自 serviceType 參數所指示的型別。This object must implement or inherit from the type indicated by the serviceType parameter.

實作

例外狀況

serviceTypeserviceInstancenullserviceType or serviceInstance is null.

此容器內已含有 serviceType 類型的服務。A service of type serviceType already exists in the container.

範例

下列範例說明如何將服務新增至 ServiceContainerThe following example illustrates how to add a service to a ServiceContainer.

m_MyServiceContainer->AddService( Control::typeid, sender );
m_MyServiceContainer.AddService(typeof(Control), sender);
m_MyServiceContainer.AddService(GetType(Control), sender)

另請參閱

AddService(Type, ServiceCreatorCallback, Boolean)

將指定的服務加入至服務容器。Adds the specified service to the service container.

public:
 virtual void AddService(Type ^ serviceType, System::ComponentModel::Design::ServiceCreatorCallback ^ callback, bool promote);
public void AddService (Type serviceType, System.ComponentModel.Design.ServiceCreatorCallback callback, bool promote);
abstract member AddService : Type * System.ComponentModel.Design.ServiceCreatorCallback * bool -> unit
override this.AddService : Type * System.ComponentModel.Design.ServiceCreatorCallback * bool -> unit
Public Sub AddService (serviceType As Type, callback As ServiceCreatorCallback, promote As Boolean)

參數

serviceType
Type

要加入之服務的類型。The type of service to add.

callback
ServiceCreatorCallback

可以建立服務的回呼物件。A callback object that can create the service. 這允許將服務宣告為可用的,但是會將物件的建立延遲至要求服務的時候。This allows a service to be declared as available, but delays creation of the object until the service is requested.

promote
Boolean

如果這個服務應該加入至任何父服務容器,則為 true,否則為 falsetrue if this service should be added to any parent service containers; otherwise, false.

實作

例外狀況

serviceTypecallbacknullserviceType or callback is null.

此容器內已含有 serviceType 類型的服務。A service of type serviceType already exists in the container.

另請參閱

AddService(Type, Object, Boolean)

將指定的服務加入至服務容器。Adds the specified service to the service container.

public:
 virtual void AddService(Type ^ serviceType, System::Object ^ serviceInstance, bool promote);
public void AddService (Type serviceType, object serviceInstance, bool promote);
abstract member AddService : Type * obj * bool -> unit
override this.AddService : Type * obj * bool -> unit
Public Sub AddService (serviceType As Type, serviceInstance As Object, promote As Boolean)

參數

serviceType
Type

要加入之服務的類型。The type of service to add.

serviceInstance
Object

要加入之服務類型的執行個體。An instance of the service type to add. 這個物件必須實作或繼承自 serviceType 參數所指示的型別。This object must implement or inherit from the type indicated by the serviceType parameter.

promote
Boolean

如果這個服務應該加入至任何父服務容器,則為 true,否則為 falsetrue if this service should be added to any parent service containers; otherwise, false.

實作

例外狀況

serviceTypeserviceInstancenullserviceType or serviceInstance is null.

此容器內已含有 serviceType 類型的服務。A service of type serviceType already exists in the container.

另請參閱

適用於