PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Client/Infrastructure/SingleViewUIService.cs

#
C# | 93 lines | 44 code | 8 blank | 41 comment | 0 complexity | e4704199c473275800cb1fbef00b354a MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System.ComponentModel.Composition;
  18. using System.ComponentModel.Composition.Hosting;
  19. using System.Windows.Controls;
  20. using MVVM.Client.Infrastructure.StateManagement;
  21. namespace MVVM.Client.Infrastructure
  22. {
  23. /// <summary>
  24. /// Implementation of the <see cref="ISingleViewUIService"/> that relies on a <see cref="IMainView"/>
  25. /// </summary>
  26. /// <typeparam name="TMainView"><see cref="IMainView"/> type</typeparam>
  27. /// <seealso cref="ISingleViewUIService"/>
  28. /// <seealso cref="IState{T}"/>
  29. /// <seealso cref="ICurrentState{T}"/>
  30. public abstract class SingleViewUIService<TMainView> : ISingleViewUIService
  31. where TMainView : UserControl, IMainView, new()
  32. {
  33. private readonly TMainView mainWindow;
  34. protected SingleViewUIService()
  35. {
  36. this.mainWindow = new TMainView();
  37. }
  38. [Import(typeof(ViewFactory))]
  39. public ViewFactory ViewFactory { get; set; }
  40. [Import(typeof(StateHandler))]
  41. public StateHandler StateHandler { get; set; }
  42. /// <summary>
  43. /// Gets the MainWindow which hosts the current view;
  44. /// </summary>
  45. public IMainView MainWindow
  46. {
  47. get
  48. {
  49. return this.mainWindow;
  50. }
  51. }
  52. /// <summary>
  53. /// Shows the view identified by <see cref="viewName"/> as the current view.
  54. /// </summary>
  55. /// <param name="viewName">The view name.</param>
  56. public void ShowView(string viewName)
  57. {
  58. var view = this.ViewFactory.GetView(viewName);
  59. this.MainWindow.CurrentView = view;
  60. }
  61. /// <summary>
  62. /// Shows the view identified by <see cref="viewName"/> as the current view, making <paramref name="context"/>
  63. /// available to views and view models.
  64. /// </summary>
  65. /// <remarks>
  66. /// <paramref name="context"/> is a plain object. It must be imported by the view associated to
  67. /// <paramref name="viewName"/> or its view model. See <see cref="IState{T}"/> for details.
  68. /// </remarks>
  69. /// <typeparam name="T">The type for the <paramref name="context"/>.</typeparam>
  70. /// <param name="viewName">The view name.</param>
  71. /// <param name="context">Context information required by the target view.</param>
  72. public void ShowView<T>(string viewName, T context)
  73. {
  74. var previousValue = StateHandler.SetState(context);
  75. try
  76. {
  77. this.ShowView(viewName);
  78. }
  79. finally
  80. {
  81. StateHandler.SetState(previousValue);
  82. }
  83. }
  84. }
  85. }