views:

1972

answers:

5

I have a simple WPF application which I am trying to start. I am following the Microsoft Patterns and Practices "Composite Application Guidance for WPF". I've followed their instructions however my WPF application fails immediately with a "TypeInitializationException".

The InnerException property reveals that "The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception."

Here is my app.xaml:

<Application x:Class="MyNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Application.Resources>         
    </Application.Resources>
</Application>

And here is my app.xaml.cs (exception thrown at "public App()"):

public partial class App : Application
{
    public App()
    {
        Bootstrapper bootStrapper = new Bootstrapper();
        bootStrapper.Run();
    }
}

I have set the "App" class as the startup object in the project.

What is going astray?

+1  A: 

Do you use .config file? If so, check it for errors. Initialization errors of such sort are often triggered by invalid XML: if there are no errors in XAML, XML config is the first place to look.

ima
+1  A: 

Thanks @ima, your answer pointed me in the right direction. I was using an app.config file and it contained this:

<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" sku="Client"/>
  </startup>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>
  </modules>
</configuration>

It seems the problem was the <startup> element because when I removed it the application ran fine. I was confused because Visual Studio 2008 added that when I checked the box to utilise the "Client Profile" available in 3.5 SP1.

After some mucking about checking and un-checking the box I ended up with a configuration file like this:

<configuration>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>
  </modules>
  <startup>
    <supportedRuntime version="v2.0.50727" sku="Client"/>
  </startup>
</configuration>

Which works!

I'm not sure why the order of elements in the app.config is important - but it seems it is.

Aydsman
A: 

Hi all! I have the same problem! Here there is the app.conf

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/ModuleTest.dll" moduleType="ModuleTest.ModuleTest" moduleName="ModuleTest">
    </module>
  </modules>
  <modules>
    <module assemblyFile="Modules/ModuleTest_b.dll" moduleType="ModuleTest_b.ModuleTest_b" moduleName="ModuleTest_b">
    </module>
  </modules>
  <startup>
    <supportedRuntime version="v2.0.50727" sku="Client"/>
  </startup>
</configuration>

The Execption is caused after I adding a second module ( ModuleTest_b ). What is the error ?

TIA.

G.

A: 

You have two sections named "modules". Place both module definitions in one section named "modules".

A: 

Hi,

I ran into a similar situation. After searching for a week time, I found the resolution and it really worked for me. It solved 2-3 problems arising due to same problem.

Follow these steps: Check the WPF key (absence) in registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation My problem was due to the absence of above mentioned key in registry.

You can modify and use following details in your registry: (Actually, you can save in file and import in registry)

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation] @="WPF v3.0.6920.1453" "Version"="3.0.6920.1453" "WPFReferenceAssembliesPathx86"="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\" "WPFCommonAssembliesPathx86"="C:\Windows\System32\" "InstallRoot"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\" "InstallSuccess"=dword:00000001 "ProductVersion"="3.0.6920.1453" "WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"

I am sure it will work.

all the best.

Regards,

Umesh

Umesh Bhavsar