When the Windows Phone XAML and XNA project template is used to create a Windows Phone 7.1 hybrid app, a region is generated within the App
class for the purpose of initializing the XNA content manager and starting a GameTimer
object. By default, a ContentManager
is exposed as a public property within your App
class, allowing it to be consumed across your application. You can, however, create new instances of the ContentManager
class if you want.
When instantiating a ContentManager
, it must be passed an IServiceProvider
that can later be used to retrieve an IGraphicsDeviceService
. The App
class implements IServiceProvider
, and the IServiceProvider.GetService
method implementation returns objects that reside in the ApplicationLifetimeObjects
collection.
Because the App
class implements the IServiceProvider
interface, it is able to pass itself to the ContentManager
constructor, providing the ContentManager
access to an IGraphicsDeviceService
, which, in this case, is a SharedGraphicsDeviceManager
. See the following excerpt:
#region XNA application initialization
// Performs initialization of the XNA types required for the application.
void InitializeXnaApplication()
{
// Create the ContentManager
// so the application can load precompiled assets.
ContentManager = new ContentManager(this, "Content");
// Create a GameTimer to pump the XNA FrameworkDispatcher.
GameTimer frameworkDispatcherTimer = new GameTimer();
frameworkDispatcherTimer.FrameAction += FrameworkDispatcherFrameAction;
frameworkDispatcherTimer.Start();
}
// An event handler that pumps the FrameworkDispatcher each frame.
// FrameworkDispatcher is required for a lot of the XNA events and
// for certain functionality such as SoundEffect playback.
void FrameworkDispatcherFrameAction(object sender, EventArgs e)
{
FrameworkDispatcher.Update();
}
#endregion
The FrameworkDispatcher.Update
method triggers event processing in the XNA framework. In a pure XNA app, it is automatically called whenever Game.Update
is called. In XAML apps, however, the absence of a Game
class means that we rely on a GameTimer
instance to raise a manual call to FrameworkDispatcher.Update
.
3.133.158.36