Problems when migrating from unity to MEF

Topics: Prism v4 - WPF 4
Dec 10, 2010 at 2:22 PM

First i invite the Prism Team to get the MEF well documented as the Unity is.

I took the HelloWorld project from the quickstart of Prism V4. And i tried to convert it from the Unity to MEF.

I've followed the guideline delivered with the Prism V4, but since the MEF is too poorly documented in it, i wasn't able to resolve the problem.

 When i lunch the debug, the shell is created and displayed but without the View!! And not exception is thrown!!!

This is my codes:

The Shell:

<Window
	xmlns:cal="clr-namespace:Microsoft.Practices.Prism.MefExtensions.Regions;assembly=Microsoft.Practices.Prism.MefExtensions"
    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	x:Class="X23P.Shell"
	x:Name="Window"
	Title="MainWindow"
	Width="640" Height="480" mc:Ignorable="d">
	<Window.Resources>
		<HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding Collection}">
			<StackPanel>
				<TextBlock Text="{Binding Property1}"/>
				<CheckBox IsChecked="{Binding Property2}"/>
			</StackPanel>
		</HierarchicalDataTemplate>
		<HierarchicalDataTemplate x:Key="ItemTemplate1" ItemsSource="{Binding Collection}">
			<StackPanel>
				<TextBlock Text="{Binding Property1}"/>
				<CheckBox IsChecked="{Binding Property2}"/>
			</StackPanel>
		</HierarchicalDataTemplate>
	</Window.Resources>

	<Canvas x:Name="LayoutRoot">
		<Grid x:Name="Main" d:DataContext="{Binding Collection[0]}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="624" Height="442">
			<Grid.RowDefinitions>
				<RowDefinition Height="0.853*"/>
				<RowDefinition Height="0.023*"/>
				<RowDefinition Height="0.124*"/>
			</Grid.RowDefinitions>
			<Grid.ColumnDefinitions>
				<ColumnDefinition x:Name="Gauche" Width="0.147*"/>
				<ColumnDefinition x:Name="Separateur" Width="0.008*"/>
				<ColumnDefinition x:Name="Droite" Width="0.845*"/>
			</Grid.ColumnDefinitions>
			<Grid.Background>
				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
					<GradientStop Color="#FF6897A5" Offset="0"/>
					<GradientStop Color="White" Offset="1"/>
				</LinearGradientBrush>
			</Grid.Background>
			<GridSplitter x:Name="VerticalSplitter" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,-0.008,0" Grid.Column="1" ShowsPreview="True" Grid.RowSpan="3">
				<GridSplitter.Background>
					<LinearGradientBrush EndPoint="3,442" StartPoint="3,0" MappingMode="Absolute">
						<GradientStop Color="#FF04C8FF" Offset="0"/>
						<GradientStop Color="Black" Offset="1"/>
					</LinearGradientBrush>
				</GridSplitter.Background>
			</GridSplitter>
			<GridSplitter x:Name="HorizontalSplitter" HorizontalAlignment="Stretch" Margin="0.008,0,8,0" Grid.Column="2" Grid.Row="1" Background="#FF742C2C"/>
            <ItemsControl x:Name="LeftRegion" cal:MefRegionManager.RegionName="LeftRegion"  Margin="0,34,0,6" />
            <TabControl Grid.Column="2" cal:MefRegionManager.RegionName="MainRegion" Height="365" HorizontalAlignment="Left" Margin="0,6,0,0" Name="MainTab" VerticalAlignment="Top" Width="507">
               
           </TabControl>
            
            <TabControl Grid.Column="2" cal:MefRegionManager.RegionName="DownRegion" Grid.Row="2" Height="48" HorizontalAlignment="Left" Margin="2,3,0,0" Name="DownTab" VerticalAlignment="Top" Width="516">
               
            </TabControl>
        </Grid>
	</Canvas>
</Window>
The MefBootstrapper:

using System.Windows;
using Microsoft.Practices.Prism.Logging;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.ServiceLocation;
//using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.MefExtensions;
//using Microsoft.Practices.Prism.UnityExtensions;

using System;



namespace X23P
{
    class X23PBootstrapper : MefBootstrapper
    {

        protected override DependencyObject CreateShell()
        {
            return new Shell();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow = (Window)this.Shell;
            App.Current.MainWindow.Show();


         
        
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(HelloWorldModule.HelloWorldModule).Assembly));

        }


    }

}



The HelloWorldModule:
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.MefExtensions.Regions;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.MefExtensions.Modularity;
using System.ComponentModel.Composition;





namespace HelloWorldModule
{
    [ModuleExport(typeof(HelloWorldModule))]

    public class HelloWorldModule : IModule
    {
        private readonly  MefRegionViewRegistry regionViewRegistry;
       
       
         [ImportingConstructor]
        public HelloWorldModule(MefRegionViewRegistry registry)
        {
            this.regionViewRegistry = registry;   
        }

        public void Initialize()
        {
        
           
            this.regionViewRegistry.RegisterViewWithRegion("LeftRegion", typeof(Views.HelloWorldView));
        }

        
    

    }
}


Developer
Dec 10, 2010 at 2:43 PM

Hi,

Have you exported your view with MEF? That might be the cause of your issue. You could also check if your module is being loaded correctly by, for example, setting a breakpoint in the Initialize method.

As for the documentation, you could check the Prism Training Kit, where there is step by step guidance on how to achieve this scenario using MEF.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 10, 2010 at 2:49 PM

Thank you guidomaliandi.

I've putted breakpoints everywhere but seems to be wrong.

The Prism Training Kit is helpfull.

How to export my view?? i didn't have a View-Model in the project!!!

Developer
Dec 10, 2010 at 2:52 PM
Edited Dec 10, 2010 at 2:57 PM

Hi,

You could export your view by putting an [Export] attribute in the code behind of your view.

You could also use the [ViewExport] attribute, which is explained in the MEF lab of the Prism Training Kit.

As an additional note, you should add the shell project to the AggregateCatalog, by adding this line to the ConfigureAggregateCatalog method:

this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(X23PBootstrapper).Assembly));

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 22, 2010 at 3:44 PM

Thank you very much, but i keep using unity. it's more simple that MEF.