How to: Communicate Between Views

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
To create client business applications using current Microsoft technologies, see patterns & practices' Prism.

This topic describes how to implement the code to provide navigation between views.

The steps in this topic assume that you have an existing smart client application that contains two views. You can use the Smart Client Software Factory 2010 guidance package to quickly create the starting point for this topic.

Ff699214.note(en-us,PandP.10).gifNote:
If you create your smart client application with a separate module that defines the layout, substitute the name of your layout module for the Shell project in the procedures that follow.

To prepare a solution for this topic

  1. Install the Smart Client Software Factory 2010.
  2. Use the Visual Studio template Smart Client Application to create the initial smart client solution. For information about how to create a solution with this template, see How to: Create Smart Client Solutions.
  3. Use the Add View (with presenter) recipe to create a view in the Shell project. Enter LeftView for the name of the view. For information about how to create a view, see How to: Add a View with a Presenter.
  4. Use the Add View (with presenter) recipe to create a view in the Shell project. Enter RightView for the name of the view.

To implement the communication between views, add the code that performs the following:

  1. Define the event topic.
  2. Publish the event from the presenter for the LeftView view.
  3. Capture a button click in the view LeftView.
  4. Refresh the view RightView.
  5. Subscribe to the event in the presenter for the RightView view.

To define an event topic

  1. Open the file EventTopicNames.cs that is located in the Constants folder of the Infrastructure.Interface project.
  2. Add the constant definition, as shown in the following code.
    public const string ChangeColor = "ChangeColor";
    
Ff699214.note(en-us,PandP.10).gifNote:
The following procedure uses rootnamespace to refer to the root namespace that you used when you created your smart client solution. Replace rootnamespace with your application's root namespace.

To create the code to publish the event

  1. Open the file LeftViewPresenter.cs.
  2. Add the following using statements.
    using rootnamespace.Infrastructure.Interface.Constants;
    using Microsoft.Practices.CompositeUI.EventBroker;
    
  3. Add the code to declare the event handler, as shown in the following code.
    [EventPublication(EventTopicNames.ChangeColor, PublicationScope.Global)]
    public event EventHandler<EventArgs> ChangeColor;
    
  4. Add the code to publish the event, as shown in the following code.
    public void ChangeViewColor()
    {
      Random random = new Random();
    
      int r = random.Next(256);
      int g = random.Next(256);
      int b = random.Next(256);
    
      System.Drawing.Color color = System.Drawing.Color.FromArgb(r,g,b);
    
      if (ChangeColor != null)
      {
        ChangeColor(this, new EventArgs<System.Drawing.Color>(color));
      }
    }
    

To capture the button click and call the presenter

  1. In Solution Explorer, right-click LeftView.cs, and then click View Designer.
  2. Drag a button from the Visual Studio Toolbox onto the LeftView view. Change the name of the button to changeColorButton.
  3. Double-click the changeColorButton button. Visual Studio creates the method changeColorButton_Click.
  4. Implement the changeColorButton_Click method, as shown in the following code.
    private void changeColorButton_Click(object sender, System.EventArgs e)
    {
      _presenter.ChangeViewColor();
    }
    

To create the code to update the right view

  1. In Solution Explorer, right-click IRightView.cs, and then click View Code.
  2. Add the method, as shown in the following code.
    void ChangeColor(System.Drawing.Color color);
    
  3. In Solution Explorer, right-click RightView.cs, and then click View Code.
  4. Add a method that changes the background color of the view, as shown in the following code.
    public void ChangeColor(System.Drawing.Color color)
    {
      this.BackColor = color;
    }
    

To create the code to subscribe to the event

  1. Open the file RightViewPresenter.cs.
  2. Add the following using statements.
    using rootnamespace.Infrastructure.Interface.Constants;
    using Microsoft.Practices.CompositeUI.EventBroker;
    
  3. Add the code to subscribe to the event and notify the view, as shown in the following code.
    [EventSubscription(EventTopicNames.ChangeColor, Thread = ThreadOption.UserInterface)]
    public void ChangeColorHandler(object sender, EventArgs<System.Drawing.Color> e)
    {
      View.ChangeColor(e.Data);
    }
    

At the conclusion of this topic, you will have the following elements:

  • An updated EventTopicNames class that contains the new event topic name
  • A view class named LeftView that contains a button (the view calls its presenter when the user clicks the button)
  • A presenter for the view LeftView that publishes an event
  • A presenter for the view RightView that subscribes to an event (when the event is received, the presenter calls the view RightView to refresh the user interface)

Show: