Monday, May 21, 2007

Stage 4: Creating and Showing the SmartPart

In the final stage, you create within the module the SmartPart that provides the view part of the Model-View-Presenter pattern. You also add a presenter for this view to the module. Then you create the connection between the module and the form in the main application to display the SmartPart in the TabWorkspace.

  1. Create an interface for the view and the presenter to communicate
    • In MyModule project, add a new Interface named IMyView.cs.
      • Add the public keyword to the interface statement.
      • declare the two members of this interface as an event named Load and a string property named Message
        public interface IMyView
        {
        event EventHandler Load;
        string Message { get; set; }
        }
  2. create a SmartPart User control
    • In MyModule project, add a User Control named MyView.cs
    • drag a Label control.
  3. implement the IMyView interface
    • In User Control MyView.cs,
      Add the IMyView interface to the class declaration
      public partial class MyView : UserControl, IMyView
    • Right-click IMyView and click Implement Interface, then click Implement Interface in the fly-out menu.
    • Replace the two throw statements that Visual Studio generates with two statements that get and set the value of the Message property
      public string Message
      { get
      { return this.label1.Text; }
      set
      { this.label1.Text = value; }
      }
  4. create the presenter class
    • add a class named MyPresenter.cs
    • make the class public
    • add a variable of type IMyView
      public class MyPresenter
      {
      IMyView view;
    • Create a constructor for the class
      public MyPresenter(IMyView view)
      {
      this.view = view;
      view.Load += new EventHandler(view_Load);
      }
    • e. Create an event handler for the Load event.
      void view_Load(object sender, EventArgs e)
      {
      view.Message = "Hello World from a Module";
      }
  5. get a reference to the WorkItem
    • In MyModuleInit.cs add the variable after myCatalogService variable declaration private WorkItem parentWorkItem
    • add a public property to the class.
      [ServiceDependency]
      public WorkItem ParentWorkItem
      {
      set { parentWorkItem = value; }
      }
    • Modify the Load method that you added during Stage 2.
      public override void Load()
      {
      base.Load();
      MyWorkItem myWorkItem = parentWorkItem.WorkItems.AddNew <
      MyWorkItem &gt (); myWorkItem.Run(parentWorkItem.Workspaces["tabWorkspace1"]);
      }
  6. create and show the view
    • In MyWorkItem.cs,
      using Microsoft.Practices.CompositeUI.SmartParts;
      Create a public Run method that accepts as a parameter a reference to the TabWorkspace.
      public void Run(IWorkspace TabWorkspace()
      {
      }
    • Add statements to the Run method that create a new instance of the MyView MyView view = this.Items.AddNew < MyView &gt();
      MyPresenter presenter = new MyPresenter(view);
    • Add statements to the Run method that add the new presenter to the current WorkItem, and call the Show method of the TabWorkspace to display the view this.Items.Add(presenter);
      TabWorkspace.Show(view);








No comments: