WPF Loose XAML ResourceDictionary - wpf

I am trying to use a ResourceDictionary in loose XAML and load it a runtime to provide templates and styles to a WPF app. I have the XAML available in a local directory and am adding a new ResourceDictionary to App.Current.Resources.MergedDictionaries in app startup using a URI.
When the XAML goes to parse, it blows up on a template where the TargetType is a custom control from the assembly that is consuming it.
The specific message is:
'Failed to create a 'Type' from the text 'controls:CustomType'.'
I already have the namespace mapped in the ResourceDictionary at the top:
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls"
There are articles out there stating that loading ResourceDictionaries from loose XAML is possible but none of them that I have found address custom types within those loose XAML files.
Any help is appreciated!

Since you are loading this at runtime and it is not built within the project then the ResourceDictionary does not know of your assembly. You will have to include the assembly in the namespace for it to be recognised.
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls;assembly=Common"
You should be able to run you app after you add ';assembly=yourAssembly' to the namespace declaration.

Related

Implicit styles in ResourceDictionaries

In NetFramework I was able to apply WPF implicit styles to a target control type by placing the styles in a ResourceDictionary, and referring to that in app.xaml.
Now that I am moving my applications to .NET6 I have found that the styles defined in a resourcedictionary are not being applied. However, if I define them in the <Window.Resources> they are applied. No doubt they would be in Application.Resources in app.xaml.
Has Microsoft made an obscure change in .NET6 that would cause this problem, or am I missing something? (I have placed the dummy string"<clr:String x:Key="DummyString">Dummy</clr:String>" in the ResourceDictionary as advised by someone.
Any help would be appreciated, as this problem is not only going to make my ResourceDictionaries obsolete, but will increase the length of my code in the .xaml file, and I will have to repeat that code in each window instead of just importing a ResourceDictionary to each project.

How to assign resource dictionary for custom controls in a dll

I have a WPF custom control MyControl.cs in my application project (.exe) with its style in a resource dictionary in MyControlResources.xaml. This xaml is specified in app.xaml as a part of the merged dictionaries. Everything works fine.
Now I want to move this custom control into an existing DLL project that the application references. Is there a way that I can create the resource dictionary "assignment" in the DLL and make it transparent to the callers i.e. the application project can use it like any built-in control that doesn't require you to know anything about resource dictionary?
I've read about creating a new custom control project can do the trick, but it's only one control for which I don't want to create a new project for. Anyone knows how to do it in an existing class library DLL?
This answer helped me find the trick.
Create a file called Themes\Generic.xaml to merge the resource dictionary.
Add the following attribute to Assembly.cs
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

WPF: OnApplyTemplate is not called anymore when I moved code into another DLL

Let's try to explain it clearly.
I've got a custom control built as a WPF application and it works fine. I've moved all the code into a external DLL. After this change, when I load the application, the method OnApplyTemplate() is not called any more and the control is not rendered either
I've try with Generic.xaml file is into a Themes directory (with capital T) in the root of the DLL which has the control and/or a Themes directory into the StartUp project.
If this info is important here's where I've found the control: http://www.codeproject.com/KB/WPF/WPFOutlookCalendar.aspx
The settings of the project is
Output type: Class Library
Target framework: .Net Framework 4
I've create a simple DLL project and I've added the references manually
Do you have any idea about the solution?
Thanks in advance...
The main difference between a standard Wpf Applicaton and a WPF Custom Control library are the following lines of code.
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Adding these lines of code to your Assembly should fix your bug.
Thanks to dowhilefor, I've found the solution!
I've recreated a new library as a WPF Custom Control Library and it works.
It is a little overkill to recreate a new project so if someone can explain to me how to reconfigure an existing project, it'd be very nice ;)

IlMerge Silverlight Class Library with Custom Controls

I am trying to merge all the assemblies of an class library in a single .dll file.
I can merge all the assemblies using the Ilmerge but is that when I use the merged dll in a Silverlight application I am having trouble when a template is apply to my control and binding problems if I use a control that inherits with UserControl.
is there any solution to solve this problem?
The problem is that when the initial dlls are built the Xaml in the project is added as a resource in the dll. The code generated to load this xaml will look something like this:-
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightLibrary1;component/MyControl.xaml", System.UriKind.Relative));
Note how the name of the dll forms part of the Uri need to fetch the xaml. I doubt IlMerge is able to spot that and fix it up. Hence once merged the Uris cannot be found.
A resolution for this is probably uglier than multiple references or simply creating another project that links all the code files involved.

WPF XAML resources

I would like to place some WPF Data Templates in a loose file that I can reference where needed in my UserControl. I do not want the Data Templates to be application wide (use app.config), I only want it to be specific to my library.
Is there a way to do this besides placing the Data Templates in UserControls and then loading the UserControls?
Thanks.
Create a ResourceDictionary and put them in that. You can then load the ResourceDictionary and access the contained DataTemplates using indexer syntax:
DataTemplate myTemplate = (DataTemplate)rd["MyTemplate"];
The ResourceDictionary is a XAML file which you would compile into your library just as you would with a UserControl or Window. If you want to ship the templates as loose (uncompiled) XAML then you can still use a ResourceDictionary, but would need to use XamlReader to load it from the .xaml source file.

Resources