WPF - using styles/resources from another assembly - wpf

I'm new to WPF and struggling to use styles that live in a separate assembly. This is what I'm doing:-
I have a class library project with a \Themes folder containing a "generic.xaml" that merges a number of xaml files from a subfolder within \Themes:-
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Metro\CoreStyles.xaml" />
... etc ...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
My solution also has a WPF application project, and in here the App.xaml merges in the resources from my library project like so:-
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyThemeLibrary;component/Themes/generic.xaml"/>
... etc...
Standard stuff so far.
Finally, I have a third project - a WPF user control library. These controls use these common styles, typically with "Style={StaticResource SomeStyle}". I can run the app and it all looks fine, but the problem is I don't get design-time support when writing the user controls - the design surface is basically empty.
Another SO article suggested adding an App.xaml to the user control library project, and merging in the resources as above. This works and I get my design-time support, however I get an error when trying to build the solution:
Library project file cannot specify ApplicationDefinition element.
I have tried changing the App.xaml build action from "ApplicationDefinition" to "Page", as has been suggested elsewhere. This gets the build working but I lose the design-time support as the user controls can no longer see the styles.
Is there a way around this problem, or failing this, an alternative way of using styles from another assembly?
Thanks in advance
Andrew

The error message says it. You cannot use "pack://application..." syntax in a library project. You should do this in your wpf project.

Related

Modern UI (WPF): Trying to use MUI for a WPF control in class library

I'm trying to use MUI framework for a existing WPF control (the WPF control is in class library). I've looked the MUI example for a modern window and tried to copy the resourceDictionary I found in the App.xaml.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
The issue that at design time, the UI seems to be have changed (look at the data)
but during running time, I'm getting exception (xamlparse exeption) that has an inner exception saying it was unable to locate the file or assembly FirstFloor.ModernUI.
I've used nuget to include the MUI framework the dll's copy to local is true.
I've also tried
<ResourceDictionary Source="pack://application:,,,/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="pack://application:,,,/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
and it doesn't work.
Any help would appreciated!
pack://application: apparently scope to the calling application. In my case the compiled dll was called from an exectuable (3rd party vendor SOLIDWORKS). I had to copy paste the FirstFloor.ModernUI dll in the folder of the SOLIDWORKS executable that solves the problem.
If anyone can provide a better answer (that doesn't involve this manually intervention), I'll tick their answer.

Shared WPF resources inside an activity designer library

I'm developing a bunch of custom activity designers, which contain custom controls, images, styles etc. The designer XAMLs are spread over several subdirectories in a library project (not a WPF application, therefore no app.xaml available)
I need a central place to store resources, just like the app.xaml in a regular WPF application.
Currently I use a projectdir\Properties\lib.xaml file like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTemplate x:Key="TrafficLight">
...
</ControlTemplate>
</ResourceDictionary>
And reference this from all my designer XAMLs like this:
<sap:ActivityDesigner.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ActDesLib;component/Properties/lib.xaml" />
</ResourceDictionary.MergedDictionaries>
where ActDesLib is the name of my assembly.
This does work, but it looks a bit clumsy. Is there a better way to do it?
Is there some "magic" app.xaml-like file that gets included automatically for a library project, without the need to add any special markup to the individual XAML files? That would make it so much easier to enforce a consistent style, even with multiple developers working on the different designers.
BTW: I tried to use relative pathes in the Source="..." attribute. This did not work when using my activities in a workflow inside VS2010, it could not find the resources then. With the absolute path, containing assembly name, it works fine. But is there no way that VS2010 or a rehosted designer can find out the path to the resource dictionary file automatically, with only relative references inside my XAMLs?

WPF Adding a 'Resource' Project to a solution with multiple WPF applications

I am developing three kiosk-like applications in WPF. They will share a similar look and feel, and I was hoping to create the projects all in the same solution. What I would like to do is add a project to the solution that just holds shared resources, such as fonts and images.
My question is, is it possible to share resources like embedded fonts across applications, and if so, what is the appropriate project type for this use? (class library? WPF user control?)
I would create a WPF Custom Control Library, then create a ResourceDictionary to hold the resources, where in the App.xaml of your start-up project I would link the Resource dictionary.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/myResourceLibrary;component/myResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
That way, you can just move the DLL around to whichever solution you want to use it in.

How to properly organize XAML resources in Silverlight?

I'm having some issues with resource files in my modular application.
I have Infrastructure.DLL and some custom controls inside this DLL. Those controls using templates from themes/generic.xaml
Issue that I have - Blend doesn't recognize those resources. Visual studio does.
Ideally I'd like to have styles for my cusom controls inside generic.xaml and styles for other controls somewhere else in common library that I can reference from my modules.
I also need Expression Blend and VS to work properly.
How do I arrange solution to make it happen?
PS. Important! WPF is different but I'm interested in Silverlight solution
You just need to create design time resource for your generic.xaml in order to let Blend recoganize it. Take a look at this post.
In each of your modules, you create a ResourceDictionary like this.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
Source="/xxx.Silverlight.Controls;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Also, in your .csproj file, you need to add this. Please note that normally this piece of code is auto-generated by Blend, so if your ResourceDictionary is auto-generated, you don't need to do the following.
<Page Include="Design\DesignTimeResources.xaml" Condition="'$(DesignTime)'=='true' OR ('$(SolutionPath)'!='' AND Exists('$(SolutionPath)') AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true')">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
<ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>
Design is the folder I created for storing my DesignTimeResources.xaml. I pretty much have the same structure as yours. :)

The resource "SearchBoxStyle" could not be resolved

I am recieving following error in VS 2010.
I have two Silverlight projects. one project only contains styles and other project is my application. I have referenced styles project in my application and user static resources from that project in my whole application, but I keep recieving this error in VS though everything works fine when I compile and run the application.
The resource "SearchBoxStyle" could not be resolved
Yes, I stumped on this one today and I am also looking for a solution.
It seems to be a missing feature of Visual Studio - VS XAML designer just cannot find static resources outside of the current XAML file.
It seems, Expression Blend is smarter and offers a workaround for such cases.
Here is a similar thread which suggests using Blend:
The resource could not be resolved (VS 2010 RC)
And here is what Microsoft says about this issue:
http://msdn.microsoft.com/en-us/library/bb546934(VS.90).aspx#ResourcesatDesignTime
And for me the solution was to put a / in the path of my resource and set the xaml file to compile as a resource. So my App.xaml looks like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/ControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Dummy Style for WPF 4 bug fix, anything you won't use goes -->
<Style TargetType="{x:Type WebBrowser}" />
</ResourceDictionary>
</Application.Resources>
It failed to load at design time when I did not use the first slash in the path, but now it works fine at design time in other XAML pages.

Resources