Adding Usercontrols To Modules

Topics: Prism v2 - WPF 3.5
Aug 4, 2009 at 2:53 PM

Im running into a problem where I have a usercontrol in a view (The usercontrol is in a different assembly), when the view is being Initialized I get a XamlParseException. My app consists of 3 projects (Shell, Module, Usercontrols). The module consists of a single view and this view references a usercontrol from the Usercontrols assembly.

The code for the view:


<UserControl ............
    <Grid x:Name="gv">

The code for this user control:
<UserControl ...........>
        <TextBlock Text="Test"/>
Now when the view is being initialized I get an error on InitializeComponent(); which is a XamlParseException :
public HelloWorldView()

But if I remove the usercontrol from the page, add a Loaded Event in the Initialization like so, and then insert the user control after the view has been loaded then it loads correctly:
public HelloWorldView()
            this.Loaded += new RoutedEventHandler(HelloWorldView_Loaded);

        void HelloWorldView_Loaded(object sender, RoutedEventArgs e)
            UserControl1 uc = new UserControl1();

So I guess my question is : What is going on? And how do I resolve this?

It seems that I need to create a dependency where the Usercontrols assembly needs to be loaded first. None the less I need to see if there is anything I can do to resolve this issue.





Aug 4, 2009 at 2:55 PM

Im also using Dynamic Modularity to load the modules.

Aug 4, 2009 at 3:03 PM

dav_evans and I collaborated on the following demo which pretty much matches your layout and requirements.

 If you can't find the answer within it perhaps you can modify it to emulate the problem you are experiencing so we have an example to work with.

Aug 4, 2009 at 6:21 PM


Thanks for the replay. Not sure that example really hits home.

Here are 2 different examples:

In Example1 youll see where I was trying to have a project of just user controls and access one of those controls in a different module within a view.

In Example2 youll see where I made the 'Usercontrol' project, a module, copied it to the Modules directory (Bin\Modules), and then created a dependcy

in the ModuleA


As im thinking about this I may be going about all this the wrong way. Maybe instead of creating an assembly of nothing but usercontrols I should just create modules with

Views that display what I want. If i need to use this view then I'll load it and place it on the screen.


The way I was approaching it was that a Module can have 1 or many Views and a View can contain 1 or many usercontrols. That being said there may be several Modules(Views)

that can use the same usercontrol, so I was going to create a project of all the common usercontrols and then use them in the views where I needed them. But with

the Dynamic Modularity approach it appears the Modules are being loaded before the Usercontrol assembly is being loaded, thus I get an error. 


Hope that helps explain it a bit more.


Aug 4, 2009 at 9:51 PM

Hi imf232,  your "examples" link doesn't appear to be working (at least under IE8).

Aug 5, 2009 at 6:16 PM
Edited Aug 5, 2009 at 8:29 PM

Sorry about that


Again the main goal that im after is this: I may have 2 different modules that share the same functionality, So I want to take that

functionality and put it into a user control (Located in an assembly of nothing but common user controls) and then use that usercontrol in both modules. Again the problem I was running into

was that the Modules seem to be getting initialized before the usercontrol assembly does which leads to an xmlparserexception.

Hope that makes more sense.

Aug 6, 2009 at 11:40 AM
Edited Aug 6, 2009 at 11:41 AM

Hi Imf232s,

I ran your Example01 DirectoryLookupModularity application and was greeted with the following error:

Cannot find type 'Usercontrols.UserControl1'. The assembly used when compiling might
be different than that used when loading and the type is missing.  Error at object
'System.Windows.Controls.StackPanel' in markup file 'ModuleA;component/defaultviewa.xaml' Line 9 Position 15.

If I removed the UserControl1 from XAML the demo ran.   I added the control back to XAML.

Note: I added the following to Properties | Build Events | Post-Build in the UsersControl project

xcopy "$(TargetPath)" "$(SolutionDir)DirectoryLookupModularity\bin\$(ConfigurationName)\Modules\" /Y


There are two ways you can resolve this.   The first (easy) way is to simply add a reference to UserControls in
your DirectoryLookupModularity project.

The second is to make your UsersControl project a module.  I added the following UserControlModule.cs file
to the UsersControl project:

namespace Usercontrols
    [Module(ModuleName = "UserControls")]
    public class UserControlModule : IModule
        public void Initialize()

I then went to my ModuleA project and updated it as follows:

namespace ModuleA
    [Module(ModuleName = "ModuleA")]
    public class ModuleA : IModule
        private readonly IRegionManager _regionManager;

        public ModuleA(IRegionManager regionManager)
            _regionManager = regionManager;

        public void Initialize()
            _regionManager.Regions["MainRegion"].Add(new DefaultViewA());