With the help of Damian Schenkelman and Julian Dominguez, we created some Visual Studio Item Templates that provides the basic assets for implementing a new view using the MVVM pattern in Prism. These templates create the necessary files and classes for the view and viewModel parts of the pattern.

I will explain in this post how to install and use the Prism MVVM templates. If you are not familiar with the MVVM pattern the following links are a good staring point:

These MVVM templates aim to help you facilitate the creation of views with presenters when developing prism applications. Then we disscuss briefly some implementation considetarions.
David Hill has recently posted the Prism Quick Start Kit which includes project templates for creating prism Modules and Shell, both for WPF and Silverlight. Damian has created code snippets for easing the creation of Commands with attached Behaviors.

How-To: Install the Prism MVVM Templates

Installing the templates it’s really simple!

  1. Dowloand templates and unzip them into your Visual Studio templates folder. They are C# templates so they go here on my system:
        My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
  2. To avoid getting a warning message the first time you use them, you can close Visual Studio and run the following command (as admin) in a Visual Studio Command Prompt window:
        devenv.exe /installvstemplates

That’s all! you should now be able see them in the New Items dialog.

How-To: Use the Prism MVVM Templates

Using the templates it’s also really simple (that’s the idea of using templates after all :-)).

  1. Open any prism application in Visual Studio. (Templates will work in any kind of application, but you will have to reference CAL assemblies).
  2. Right click on the project/folder where you want to place your new view and select Add / New Item…The New Item Dialog will appear.
  3. Select Prism MVVM or Prism MVVM (with interfaces) from the My Templates category.image
  4. Click Add.  This will create the View with xaml and code behind, and the ViewModel. If you selected Prism MVVM (with interfaces) template, it will additionally create interfaces for the View and ViewModel (this is highly recommended as it will ease testing).

image

Great, we have now the new view with it’s corresponding ViewModel.  To show the view you should register it in the appropriate region as usual. For example, using view discovery (if you didn’t used interfaces, you won’t need the first line):

this._container.RegisterType<ICustomersViewModel, CustomersViewModel>();
this._regionManager.RegisterViewWithRegion(RegionNames.ButtomLeftRegion, () => this._container.Resolve<CustomersView>());

Using the above code in David’s Quickstart Solution, you can get the view shown:

image

Implementation considerations:

You will notice in the implementation of the view and viewModel that:

  1. View-First approach is used.
  2. Templates can be used in WPF and Silverlight prism applications.
  3. The implementation relies on prism, as it inyects the EA to the ViewModel.
  4. A property is created in the viewModel, to demostrate databinding in the view.

You can download the templates from here.
kick it on DotNetKicks.com

David Hill (patterns and practices architect) has been working recently on new Visual Studio Templates that provide an excellent starting point for WPF and Silverlight Prism-v2 applications.  This is an experiment on how p&p could provide some level of automation for next versions of prism, so don’t hesitate to post any comment on David’s post.

You can download and install these templates from David’s blog:

The Composite Application Guidance for WPF and Silverlight (a.k.a Prism) provides several Quickstarts that demonstrate key concepts of the library and the Stock Trader Reference Implementation which illustrate a real-world scenario. The Prism Quick Start Kit aims to provide the basic structure for prism projects and solutions, easy to understand and make use of the recommended practices.

The Prism Quick Start Kit comes with quick-start solutions, templates for the Shell and module projects and supports both WPF and Silvelright.

Hope you find them useful!

The IRegion interface allows to Add/Remove and Activate/Deactivate the views contained in the region. These two sets of actions let us manipulate the state of each view in regions. While the meaning of Add/Remove is clear, the semantics of Activate/Deactivate might be confusing as it greatly depends on the concrete implementation of IRegion.

Activate semantic by concrete region implementation (between brackets is the type of control attached to by default):

  • SingleActiveRegion (ContentControl): There is a maximum of one active region at a time. This means that activating a view might trigger a deactivation of another view.
    If the region is created with the default adapter (ContentControlRegionAdapter) the active view will also be only one visible, as it is set as the content of the ContentControl.
  • AllActiveRegion (ItemsControl): All views that are kept in the region are active. Calling Deactivate on a view will throw an InvalidOperationException. All views are usually visible.
  • Region (Selector): This region allows for multiple active and deactive views.
    If created with the default adapter (SelectorRegionAdapter) the active views will be kept in sync with the SelectedItem/SelectedItems of the control. So when calling Activate on a view, you can only select a single active view at a time. By setting the SelectedItems property of a listbox, you can set multiple views to active. All views (active and deactive) are visible.

The RegionActiveAwareBehavior and the IActiveAware interface are very close related to the activate semantic. So for example, even if the IActiveAware interface is implemented in a view, if it is contained in an AllActiveRegion, the view will only be notified of the active state change only once (when added to the region).

Hiding views

Activating and deactivating views is usually confused with it’s visibility. If you deactivate a view, it will only hide it if the region is a SingleActiveRegion. It will throw an exception in AllActiveRegions and it will not hide the view in Regions.

The posibility of hiding views while keeping the view in the region is not implemented out-of-the-box in prism-v2. Hiding view only makes sense in ItemsControl containers, as in other containers views are automatically hidden when they are not active. Hiding views might be particularly helpful, for example if there is a requirement to hide certain TabItems (as deactivating the view won’t hide the tab, and setting Visibility=Hidden in the view won’t hide the tab).

With Damian Schenkelman, we created the following two classes (one for Silverlight and one for WPF) that add extension methods to allow hide/show views in prism-v2 regions (Download links below). WPF methods implements hide/show for all ItemsControl containers while Silverlight methods are only implemented for the TabControl.

· SilverlightRegionExtensions (allow hide views in TabControl regions)

· WPFRegionExtensions (allow hide views in ItemsControl regions)

The aforementioned classes expose the following extension methods for the IRegion interface:

· Hide(string viewName): Hides a view registered with a particular name in the region.

· Hide(object view): Hides the view passed as a parameter.

· Show(string viewName): Shows a view registered with a particular name in the region.

· Show(object view): Shows the view passed as a parameter.

We also created a sample application to show it’s usage and to highlight the difference betwen Add/Remove, Activate/Deactivate, Hide/Show.
image

You can play with it selecting the tab checkboxes and pressing the action buttons.

 

Download

We uploaded the WPF and silverlight region extension method to the Prism codeplex site. You can get these classes from RegionExtensions.zip.

You can also downloaded the sample application from Hide Views Sample .
kick it on DotNetKicks.com

Yesterday, while developing a demo app that uses Prism-v2, WCF services, ASP.NET web application and interaction with WebBrowser controls ,  I run into a little problem trying to consume the service from a module.  I thought could be useful posting about it to save someone a little time.

The Problem

I had a WCF service in place. It was working fine with a ASP.NET web app, but when I tried to cunsume it from a Prism-v2 module I got the following exception:

‘System.InvalidOperationException: Could not find default endpoint element that references contract ‘CustomerService.ICustomerService’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

image

I was using the app.config that was generated by the WCF “add service reference” wizard. I checked it’s content but everything was fine.

The solution

Move the app.confing to the Shell project!
In prism modules are loaded dynamically, so they all run in the Shell project context. WCF was not finding my app.config because it was not in the right place.

image

 

This will do for simple applications, but most general application already have an app.config with settings. On the other hand, modules should be completely decoupled from the shell. A good way to resolve these issues is to set WCF configuration within the module programatically.

I hope this is useful when consuming WCF Services from Prism Modules.
 

kick it on DotNetKicks.com

I’ve seen that there are quite a few questions on how to use Composite Application Guidance for WPF and Silverlight  (a.k.a Prism) with XBAP applications on the discussion list, so I’ll try show you in this post how to migrate the HelloWorld demo (included with CAL) to XBAP.  If you are in a hurry, you can skip directly to the Solution.

Problem

The main issue is that XBAP applications rely on WPF framework to create the NavigationWindows that will be holding XBAP pages and CAL needs to create the main window on the Bootstrapper (creating instances of NavigationWindows is not allowed in PartialTrust applications).

The first thing that you will  do to convert your WPF application into an XBAP application is changing <Window> for <Page> in you XAMLs. In most scenarios that is practically all you have to do because WPF framework is intelligent enough to create the right environment for the application: If you are using Windows, it just creates a new instance of a window and calls it’s Show method (like you usually do in the CAL’s Bootstrapper). But if you are using Pages, it needs to host pages in NavigationWindows or the Browser.

But, if you are using CAL after changing your Windows for Pages, you will end up commenting the call to the Show Method (pages don’t have a Show method) and setting the starupURI of the application. And that’ were the root of the problem lies: WPF creates an instance of a Page and you are creating another instance in the Bootstrapper. That will mess up things and you will end up loosing references to the CAL’s regions and contentControls.

Solution

Based on Mokosh’s Prism and WPF Browser Application (Xbap) post that shows a good way run Prism v1 solution as XBAPs,  let’s migrate a Prism v2 solution to run as an XBAP application is as follows:

We will start with the HelloWorldDemo.Desktop Quickstart included in Prism v2.

  1. Set the shell as the startup page of the application. To do that, open the App.xml file and add StartupUri=”Shell.xaml” to the <Application> element.
    <Application x:Class=HelloWorld.App
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    StartupUri=Shell.xaml>
      <
    Application.Resources>
      </
    Application.Resources>
    </
    Application>
  2. In the App.xaml code behind (App.xaml.cs) remove the OnStartup method. We will add the BootStrapper initialization afterwards.
  3. In the Shell.xaml file change the <Window> element for a <Page> element. You should end up with the Shell.xaml code as follows:
    <Page x:Class=HelloWorld.Shell
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:cal=http://www.codeplex.com/CompositeWPF
    Title=Hello WorldHeight=300Width=300>
      <
    ItemsControlName=MainRegion/>
    </
    Page>
  4. In the Shell.cs make Shell inherit from Page instead of Window. Ej: public partial class Shell : Page
  5. To avoid creating a new instance of the shell, get the one created during the Application startup.  A good place to do that is the Shell constructor.
  6. public Shell()
    {
        Bootstrapper bootStrapper = new Bootstrapper();
        bootStrapper.ShellPage = this;
        bootStrapper.Run();
    
        InitializeComponent();
    }

  7. Now we will need to add a property to the Bootstrapper class to have a reference to the Shell and  then return it in the CreateShell method. So the complete Bootstrapper code should be:
  8. class Bootstrapper : UnityBootstrapper
    {
        private DependencyObject shellPage;
        public DependencyObject ShellPage
        {
            get
            {
                return shellPage;
            }
    
            set
            {
                shellPage = value;
            }
        }
    
        protected override DependencyObject CreateShell()
        {
            return ShellPage;
        }
    
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog()
                .AddModule(typeof(HelloWorldModule.HelloWorldModule));
            return catalog;
        }
    }

  9. Press F5 and Enjoy!

Further improvements:

clip_image002 clip_image004

Technorati Tags: Patterns and Practices,Composite Application Guidance,WPF,XBAP,Prism,CAL
kick it on DotNetKicks.com

SQL Services

November 8th, 2008

Comming soon!

 

.Net Services

November 8th, 2008

Comming Soon!

 

Windows Azure

November 8th, 2008

 

Windows Azure is part of the Azure Services Platform which I introduced in my previous post.

This new platform will allow us (Developers) to run your applications directly in the cloud by hosting them in Windows Azure. This means that not only Web apps can be developed for Windows Azure but also background processes are also supported.

Windows Azure not only provides an environment to run applications but it also provides a platform for storing application’s data. This data is exposed using a RESTful approach, which means that applications running on Azure and applications running elsewhere can access this data in the same way.

 

 

Up to this date Windows Azure can run only applications built on the .NET Framework, but Microsoft has announced that it will also support running unmanaged code in 2009.

 

Why Use Windows Azure?

Windows Azure runs on a large number of machines, located in the huge Microsoft’s data centers. By running applications on the cloud, we are making use of the advantages of Microsoft’s infrastructure  which can have great benefits:

·         Save money. Instead of buying your own servers (even when you need your app to scale), use the ones provided by Azure and pay just for what you use .

·         Save Time. Installing and maintaining systems and servers take time and effort. Microsoft does this for you!

 

How to start?

The best way to start learning and coding for Windows Azure is doing the Training Kit Labs:

   

 

 

Azure Services Platform

October 31st, 2008

As everybody must already know, Microsoft annouced on PDC2008 it’s new cloud services platform called Azure Services Platform:

 

What is it?

Azure Services Platform (or just Azure) is a group of cloud technologies, at the moment composed by an Operating System (Windows Azure) and a set of services (.Net Services, SQL Services, Live Services, Sharepoint Services and Dynamics CRM Services).

 

 

Azure was built flexible enough to allow the two possible scenarios: Building new applications to be run in the cloud; Enhance already existing applications (web, desktop and mobile) by consuming cloud services.

Azure makes all this services available through open, standard, interoperable protocols such as HTTP, REST, SOAP, etc. This enables end users and both Microsoft and non-Microsoft programming languages to consume this services. In the near future also non Microsoft development environments will be released (such as Eclipse!).

 

 

Before getting deeper with each of the services provided, remember you can start coding and using this platform yourself!

  •  A good place to start coding is the Azure Training Kit: download it here.
  •  And a good technical overview of the hole platform can read from David Chappell  White Paper: Introducing the Azure Services: download it here.

 

To know each service a bit deeper you can check my other posts:

Windows Azure:
 http://blogs.southworks.net/matiasb/2008/11/08/windows-azure 

.Net Services:  
 http://blogs.southworks.net/matiasb/2008/11/08/net-services/

SQL Services:
 http://blogs.southworks.net/matiasb/2008/11/08/sql-services

Live Services:  
 http://blogs.southworks.net/matiasb/2008/11/08/live-services/