Wednesday, May 2, 2012

Prism-Composing the User Interface ( View Injection vs View Discovery)

View Injection

IRegion.Add
public void Initialize()
{
    _regionManager.Regions["MainRegion"].Add( new ModuleAView() );
}

View Discovery

IRegionManager.RegisterViewWithRegion
public void Initialize()
{
    _regionManager.RegisterViewWithRegion( "MainRegion", typeof( ModuleAView ) );
}   

When to Use View Discovery vs. View Injection

 

Choosing which view loading strategy to use for a region depends on the application requirements and the function of the region.
Use view discovery in the following situations:
  • Automatic view loading is desired or required.
  • Single instances of a view will be loaded into the region.
Use view injection in the following situations:
  • Your application uses the Navigation APIs.
  • You need explicit or programmatic control over when a view is created and displayed, or you need to remove a view from a region; for example, as a result of application logic or navigation.
  • You need to display multiple instances of the same views in a region, where each view instance is bound to different data.
  • You need to control which instance of a region a view is added to. For example, you want to add a customer detail view to a specific customer detail region. (This scenario requires implementing scoped regions as described later in this chapter.)
Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;

namespace HelloWorldModule
{
    public class HelloWorldModule:IModule
    {
        private readonly IRegionManager regionManager;
        public void Initialize()
        {
            //View Injection
            regionManager.Regions["MainRegion"].Add(new Views.HelloWorldView());

            //View Discovery
            regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));

          
        }
      public  HelloWorldModule(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
        }

    }
}

No comments:

Post a Comment