Tuesday, May 29, 2007

Stage 4: Creating and Showing the SmartPart

create an interface for the view and the presenter to communicate

Create a Interface named IMyView.cs in the MyModule project and change as .
public interface IMyView
{
event EventHandler Load;
string Message { get; set; }
}
Create a SmartPart user control
  1. add a User Control named MyView.cs
  2. Drag a Label control.
  3. Implement the IMyView interface
    1. add IMyView interface t othe class MyView
      public partial class MyView : UserControl, IMyView
    2. right-click IMyView and click Implement Interface, then click Implement Interface in the fly-out menu.
    3. modify property for Message in Interface.
public string Massage
{
get { return this.label1.Text;}
set { this.label1.Text = value;}
}
    Create the presenter class
    1. Create a class named MyPresenter.cs
    2. add a variable of type IMyView
      public class MyPresenter
      {
      IMyView view;
    3. Create a contructor for the class
      public MyPresenter(IMyView view)
      {
      this.view = view;
      view.Load += new EventHandler(view_Load);
      }
    4. Create a event handler for the Load event
    void view_Load(object sender, EventArgs e)
    {
    view.Message = "Hello World from a Module";
    }
    get a reference to the WorkItem
    1. Open the MyModuleInit.cs add the following after the myCatalogService declaration.
      The variable will contain a reference to the root ShellWorkItem
      private WorkItem parentWorkItem;
      [ServiceDependency]
      public WorkItem ParentWorkItem
      {
      set { parentWorkItem = value; }
      }
    2. Modify the load method.
      public override void Load()
      {
      base.Load();
      MyWorkItem myWorkItem =
      parentWorkItem.WorkItems.AddNew<MyWorkItem>();
      myWorkItem.Run(parentWorkItem.Workspaces["tabWorkspace1"]);
      }

    Create and show the view

    1. In MyWorkItem.cs, add
      using Microsoft.Practices.CompositeUI.SmartParts;
    2. Create a public Run method that accepts as a parameter a reference to the TabWorkspace.
      public void Run(IWorkspace TabWorkspace)
      {
      IMyView view = this.Items.AddNew<MyView>();
      MyPresenter presenter = new MyPresenter(view);
      this.Items.Add(presenter);
      TabWorkspace.Show(view);
      }

    No comments: