IScreenFactory.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // IScreenFactory.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. namespace GameStateManagement
  11. {
  12. /// <summary>
  13. /// Defines an object that can create a screen when given its type.
  14. ///
  15. /// The ScreenManager attempts to handle tombstoning on Windows Phone by creating an XML
  16. /// document that has a list of the screens currently in the manager. When the game is
  17. /// reactivated, the ScreenManager needs to create instances of those screens. However
  18. /// since there is no restriction that a particular GameScreen subclass has a parameterless
  19. /// constructor, there is no way the ScreenManager alone could create those instances.
  20. ///
  21. /// IScreenFactory fills this gap by providing an interface the game should implement to
  22. /// act as a translation from type to instance. The ScreenManager locates the IScreenFactory
  23. /// from the Game.Services collection and passes each screen type to the factory, expecting
  24. /// to get the correct GameScreen out.
  25. ///
  26. /// If your game screens all have parameterless constructors, the minimal implementation of
  27. /// this interface would look like this:
  28. ///
  29. /// return Activator.CreateInstance(screenType) as GameScreen;
  30. ///
  31. /// If you have screens with constructors that take arguments, you will need to ensure that
  32. /// you can read these arguments from storage or generate new ones, then construct the screen
  33. /// based on the type.
  34. ///
  35. /// The ScreenFactory type in the sample game has the minimal implementation along with some
  36. /// extra comments showing a potentially more complex example of how to implement IScreenFactory.
  37. /// </summary>
  38. public interface IScreenFactory
  39. {
  40. /// <summary>
  41. /// Creates a GameScreen from the given type.
  42. /// </summary>
  43. /// <param name="screenType">The type of screen to create.</param>
  44. /// <returns>The newly created screen.</returns>
  45. GameScreen CreateScreen(Type screenType);
  46. }
  47. }