Archive for January, 2010

Silverlight MVVM Databinding for Blendability (View First)

Once you have a View (xaml interface design with no code behind), a ViewModel (class that contains the logic to connect the View and the Model) you’ll want to bind data to the controls.

The following sets the DataContext to the ViewModel





As this is in the XAML you will be able to view the results in Blend.

Advertisements

MVVM and Prism – The App, BootStrapper, Shell and Modules

Prism helps create a modular application using something called the Bootstrapper.  This can initiate the Shell (a UserControl that contains Regions where you can place your Modules) and also initiates the ModuleCatalog which manages you modules – opening, closing, positioning, etc.

In the App.xaml.cs change the Application_Startup to read:

<div id="_mcePaste">private void Application_Startup(object sender, StartupEventArgs e)</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">Bootstrapper b = new Bootstrapper();</div>
<div id="_mcePaste">b.Run();</div>
<div id="_mcePaste">}</div>
private void Application_Startup(object sender, StartupEventArgs e)
{
Bootstrapper b = new Bootstrapper();
b.Run();
}

In the Bootstapper you can initiate the RootVisual as the Shell and create a ModuleCatalog and add any modules.


namespace MVVM_Example
{
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = Container.Resolve();
Application.Current.RootVisual = shell;
return shell;
}

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleA.ModuleA));
return catalog;
}
}
}

Each module is a separate project that can a number of Views with their ViewModels.

The module needs to be registered with a region.

namespace ModuleA
{
public class ModuleA : IModule
{
IRegionManager _regionManager;
IUnityContainer _container;

public ModuleA(IRegionManager regionManager, IUnityContainer container)
{
_regionManager = regionManager;
_container = container;
}

#region IModule Members

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

}

#endregion
}
}