1

I have a single Winforms project and multiple WPF projects in one solution. From the Winform application I'd like to open one of the WPF Windows (it's a MetroWindow from Mahapps, if it matters).

After looking at the accepted answer to this stackoverflow question I ended up with this piece of code:

OpenWPFAppButton_Click(object sender, EventArgs e)
{
    WPFApp wpfApp = new WPFApp();
    ElementHost.EnableModelessKeyboardInterop(wpfApp);
    wpfApp.Show();
}

Unfortunately if I click the button a XamlParseException occurs, which points to the first Style="{StaticResource ... }" line in WPFApp.xaml (the Main Xaml File).

enter image description here

Does this mean I cannot open WPF Windows from Winforms that include static resources? Or am I missing something simple here?

EDIT: Here is the content of the App.xaml file:

<Application x:Class="WPFAppProjectName.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         StartupUri="WPFApp.xaml">
<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/ButtonStyles.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/MenuStyles.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/TextBoxStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

Community
  • 1
  • 1
betaFlux
  • 183
  • 1
  • 3
  • 12

1 Answers1

1

It is most likely that some common resources are defined in the App.xaml. That file isn't loaded when running through Windows Forms and thus those resources are unavailable. Hence you get this error.

You could include the resource definitions in the Window.xaml files, or an own common style resource file (aka Resource Dictionary) which is included in every Window.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks for your help! I have added the App.xaml code to my question. There you can see the links to some resource packs. Could you please tell me, how to include these directly in my Window.xaml file? Is this even possible? – betaFlux Nov 16 '14 at 20:20
  • Sure. Copy the entire code between `Application.Resources` and copy that into your `Window`. Rename `Application` to `Window`. – Patrick Hofman Nov 16 '14 at 20:22
  • When I copy the code between `Application.Resources` there is no `Application` to rename or do you mean everything including the `Application.Resources` tags? – betaFlux Nov 16 '14 at 20:41