XamlParseException when attempting to open a WPF Window from Winforms - c#

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).
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>

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.

Related

Window style not visible in designer

I created a Window style (WPF) and added it as a dll to my project
this style shows corretly when i run the program but doesn't show up in the designer.
I googled already but none of the solutions there are working
Test 1:
// Window //
Style="{DynamicResource HVE_Window}"
// Window.Resources //
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
Result:
Error: 'Window' TargetType doesn not match type of element 'WindowInstance'
-> But it runs and display correctly there
Test 2:
// Window //
Style="{DynamicResource MyWindow}"
// Window.Resources //
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MyWindow" TargetType="{x:Type Window}" BasedOn="{StaticResource HVE_Window}" />
Result:
No Error:
Still doesn't display in the designer, still shows up if i run the program
Test 3:
Both versions but added to application resources
How it should look:
How it looks inside the designer:
You can sometimes find that resources from a control library are not loaded at design time, despite whatever you put in app.xaml to try and load the things.
MS created a mechanism for Blend which you can use in visual studio since it's the blend designer.
This uses a "special" resource dictionary called DesignTimeResources.xaml
This will only be used at design time.
Add one to the Properties of your problem exe project.
With exactly that name.
Put all your merges into that.
eg this is one of mine from my MapEditor project that uses numerous resources from UILib. UILib is a control library with all sorts of UI stuff in it.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MapEditor">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/Geometries.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/ControlTemplates.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/FontResources.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/UILibResources.xaml"/>
<ResourceDictionary Source="/Views/Drawing/Terrain/Resources/CityResources.xaml"/>
<ResourceDictionary Source="/Resources/MapEditorResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Unload your csproj ( right click in solution explorer), edit it and find the node for that resource dictionary.
Change it to:
<Page Include="Properties\DesignTimeResources.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>
Reload the project, close and re-open visual studio.
Your styles should now apply.

Cannot find ResourceDictionary when editing copy of template

please forgive me in advance. First question.
I am working on a WPF project where I have defined a simple resource dictionary at the application level.
<Application x:Class="Game.UI.Modals.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="GameResources.xaml"></ResourceDictionary>
</Application.Resources>
The reference to the ResourceDictionary is working fine; I am able to utilize it in XAML with no issues.
The problem I am having is in trying to add templates to the dictionary using the Create ControlTemplate Resource tool (right click => edit template => create empty). The Resource dictionary radio button in the define in panel is grayed out.
I have tried creating dictionaries in different namespaces, I made sure the dictionary is named and that the build action is set to resource.
Any idea how to get the Create ControlTemplate Resource dialog to recognize my application level ResourceDictionary?
Thanks!
Include your GameResources.xaml like this instead:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GameResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The create template dialog lists the merged dictionaries. When you include your resources as in my example, the radiobutton should become enabled and your merged dictionary should appear in the list.

WPF Designer Failing to Load Generic.xaml

I have written a WPF Class Library. It has a Generic.xaml file under a themes folder in the project and also a ThemeInfo attribute in the AssemblyInfo.cs file:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
All works well, except in the designer. I get blue squigly lines anywhere I am using StaticResource to reference my brushes, styles and other resources saying:
The resource '[Resource Name]' cannot be found.
I really want the designer to pick up my Generic.xaml file and show the controls as I have styled them. How can I achieve this?
UPDATE
I have marked Yogesh's answer as correct but here is some more information. I was adding the resource dictionary in the constructor of the App.xaml file, instead of in the xaml. The XAML designer does not seem to execute the code behind for the App.xaml file.
Just add a new page named App.xaml with Application as the root element in the class library with Build Action set to Page. Now add the generic.xaml file as a resource dictionary. Something like this...
<Application x:Class="[YourNamespace].App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="[AbsoluteOrRelativePath]/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
After you do this, rebuild your project, close all xaml views and reopen them again. This should fix your issue in VS2012/2013 and Blend 2012.

Unable to load resource image from another assembly

Case
I've got two assemblies: one holding the application, one acting as library.
The library holds and image resource, say: "Images/image.png" with its build action set to resource.
Library
Next to that, the library contains a SomeStyle.xaml file:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Image x:Key="someKey" Source="/LibraryAssemblyHere;component/Images/image.png" />
</ResourceDictionary>
Application
The application assembly has the App.xaml with:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LibraryAssemblyHere;component/Styles/SomeStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Problem
When starting the application, Visual Studio 2010 gives me the following error:
Failed to create a 'ImageSource' from the text '/LibraryAssemblyHere;component/Images/image.png
With an inner exception:
Cannot locate resource 'images/image.png'.
Question
Simple: What am I doing wrong? I've searched and searched, but nothing helped.
Thanks in advance!

Create Application from code and load resource dictionnary from xaml

This might be an extremely dumb question, but I simply can't understand the problem at the moment.
I have an App.xaml file that defines application level resources :
<Application x:Class="WpfMPManager.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionnary.xaml"/>
</ResourceDictionary>
</Application.Resources>
For various reasons, I start this application through a .cs file like that (there actually is a lot more going on in this file).
[STAThread]
static void Main(params string[] args)
{
App myApp = new App();
MainWindow myWindow = new MainWindow();
myApp.Run(myWindow);
}
However, when I start the application in this way, my application resource dictionnaries are empty (and they are indeed filled if I start the application through my App.xml).
Should I call a specific method on the Application object to force it to load the resource dictionaries defined in the .xaml file ?
Thanks in advance.
Answering my own question in case anyone stumbles on that
It seems it's enough to call myApp.InitializeComponent() which is not called by the default constructor. Could have figured it out faster.

Categories

Resources