Partager via


GameServiceContainer.AddService Method

Adds a service to the GameServiceContainer.

Syntax

'Declaration
Public Sub AddService ( _
         type As Type, _
         provider As Object _
)
public void AddService (
         Type type,
         Object provider
)
public:
void AddService(
         Type type,
         Object provider
)

Parameters

  • type
    Type: Type
    The type of service to add.
  • provider
    Type: Object
    The service provider to add.

Exceptions

Exception type Condition
ArgumentNullException The type or provider parameter is null.
ArgumentException The container already contains a service of this type, or the service provider object of the type requested is not assignable to this service type.

Remarks

Game services are a mechanism for maintaining loose coupling between objects that need to interact with each other. Services work through a mediator—in this case, Game.Services. Service providers register with Game.Services, and service consumers request services from Game.Services. This arrangement allows an object that requires a service to request the service without knowing the name of the service provider.

Game services are defined by an interface. A class specifies the services it provides by implementing interfaces and registering the services with Game.Services. A service is registered by calling Game.Services.AddService specifying the type of service being implemented and a reference to the object providing the service. For example, to register an object that provides a service represented by the interface IMyService, you would use the following code.

Services.AddService( typeof( IMyService ), myobject );

Once a service is registered, the object providing the service can be retrieved by Game.Services.GetService and specifying the desired service. For example, to retrieve IGraphicsDeviceService, you would use the following code.

IGraphicsDeviceService graphicsservice = (IGraphicsDeviceService)Services.GetService( typeof(IGraphicsDeviceService) );

Requirements

Namespace: Microsoft.Xna.Framework

Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

See Also

Reference

GameServiceContainer Class
GameServiceContainer Members
Microsoft.Xna.Framework Namespace

Platforms

Windows Phone