Resources from ResourceDictionary accessible but Styles are not - wpf

I'm hoping to create a WPF User Control Library with reusable controls for my many projects to save time recreating them every time (something I've found myself doing).
So I:
Created a WPF User Control Library, and created a ThemeDark.xaml file to contain common colour keys and styles.
Created a separate regular WPF project in the same solution so that I can test the controls in an actual window.
Added a project reference to the User Control Library.
Added a ResourceDictionary.MergedDictionaries into the App.xaml which references the theme file from the other project.
The problem: I can access and use the SolidColorBrush keys from the theme file just fine in the testing project, but for some reason IntelliSense can't see the Styles (which all have keys).
I also can't seem to use the pack://application:,,,/ method of referencing the ResourceDictionary in App.xaml; it only accepts the filename. This was originally set through the Properties panel -> Source -> Browse, however I have tried a few different formats, all to no avail. It also doesn't seem to notice that ThemeDark.xaml is in a Themes/ directory within the User Control Library project.
So - How does one properly reference a ResourceDictionary from a WPF User Control Library project and be able to use all resources (including Styles)?
DarkTheme.xaml (inside user control library project):
App.xaml (inside testing project):
Resulting behaviour (very strange - SolidColorBrushes available but Styles aren't):

I found the problem. I had removed the spaces from the project's namespace, but not from the assembly name.
Once that was fixed I was able to use Source="pack://application:,,,/MyControlLibrary;component/Themes/ThemeDark.xaml"
MyControlLibrary isn't the actual name! ;)

Related

In WPF Project I have separate folder for each module I want default app.xaml file to be loaded whenever I hit that module screen

enter image description hereI am migrating my project from Silverlight to WPF and In Silverlight each module has different project and it's own configuration file i.e. App.xaml. But while migrating it in my WPF project I have separate folder for each modules. In WPF we are adding silverlight library files into WPF folder. So how we can initialize app.xaml file in WPF at folder level?
The answer to " So how we can initialize app.xaml file in WPF at folder level?" is:
You cannot do that.
You might be able to do something similar will satisfy your requirements. Assuming you're talking resources.
Consider a single resource in a resource dictionary with an x:Key of "XX".
When you merge that in app.xaml it's added to a hashtable with a key of XX and value of whatever your resource is. That goes into Application.Current.Resources.
If your wpf app then loads any usercontrol, window whatever that uses a dynamicresource XX then it gets that value. Doesn't matter where it is loaded from. That could be a different library or whatever you like.
Resources have scope.
You could therefore merge a resource dictionary at window level. Say you added another resource dictionary in a module. You merge it into Window1's resources and it has something with a x:Key of "XX". Anything in that instance of Window1's visual tree will grab that "new" XX you just merged in. Anything in any other window will still be using that one out of Application.Current.Resources.
You could conceivably merge resource dictionaries at usercontrol level in this way. This is usually a bad idea though because each gets an instance in memory of your resources. That can add up to a lot of memory if you're not careful.
You could therefore arrange things so each module has it's own window ( or some other parent ) which merges in it's own somehow unique resource dictionary. That supplies a more local version of any resources you need.
Or
Maybe the user only navigate to and hence sees one module at a time. Whatever a module is.
You could then merge your resource dictionaries in at application level. Everything gets them then though.

WPF Designer doesn't find dll-resources

Context:
I have a created a user control library project in Microsoft Visual Studio 2017 with a lot of Styles and ControlTemplates to be used in various future projects. Now I want to reference it in a new solution.
What works:
When I create a new solution with a new WPF-Application I can import the user control library project into the solution and add a reference to it in my new WPF-Application project - everything works fine this way. But this isn't a good solution for me because I don't want to copy the sourcecode everytime I use it.
What doesn't work:
...So I tried to not import it and referencing it as a project but to just reference the compiled dll-file of the user control library. In principle, everything should stay the same from the WPF-Applications point of view. But the Designer doesn't find the referenced resources correctly. At runtime it works perfectly well with no errors, but at designtime the Designer either ignores the referenced resources or crashes entirely.
Its behaviour doesn't seem to be very consistent anyway:
It finds some resources correctly, e.g. It shows referenced ColorBrushes defined in the user control library correctly (e.g.Background="{StaticResource MyBlue}")
Sometimes it seems to find a referenced Style-resource but not the resources nested within that Style (e.g. Style="{StaticResource TextBoxOutput}" shows error "The resource "MarginStandard" can't be found." when MarginStandard is used in TextBoxOutput)
Styles without a key which should apply by default don't apply automatically.
All this works correctly at runtime and when I reference the user control library as project rather than as dll.
What I already checked
User control library and WPF-Application use the same CPU-Architecture (AnyCPU).
User control library and WPF-Application use the same .net Version (4.6.1)
I tried different Build-Actions for compiling the user control library (resource, embedded resource, none).
The dll-file gets copied into the bin/debug-folder of the WPF-project.
All resource dictionaries from the user control library are imported in the App.xaml file of the WPF-project using the following Syntax:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UserControlLibraryName;component/RecourceDictionaryName.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
My Question
How can I use resources referenced as dll-file in such a way that the Designer also finds the resources correctly? My Styles and ControlTemplates alter the look of the Controls drastically, so its really necessary to be able to see the results at designtime.

WPF: Make Application Resources override usercontrol library resources

I have a wpf application project, with user controls in a separate project.
I'd like to have different resource dictionaries in the different projects
When I view the user controls from the user control project, I'd like to use the user control library's dictionary.
when I run the Application, i'd like to use the application's dictionary.
Right now, when i run the application, the application still has it's own dictionary, but the user controls are still using the user control library's dictionary
I've tried combinations of dictionaries and merged dictionaries.
Not really sure how to do this.
thanks in advace
So I found a workaround:
Include an App.xaml file in the user control library. Define the user control design time resources in the user control App.xaml file
In the regular wpf application project, include the run time resources.
At design time, the user controls only see its own App.xaml file, while at run time, the appliction is run and uses the resources defined in the application projects App.xaml file
Don't know if this is the optimal method of doing this. If anyone has any comments on doing it this way, please feel free to post
Thanks

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

Resource Dictionaries in a Silverlight Assembly?

I've just begun dabbling in putting together a set of controls as assemblies and I'm working on default styling. What I currently have is a UserControl in a project (thanks Reed!) and I'm able to bring that into another project via reference. I plan to add more controls over time to build something of an SDK.
I currently have some hooks that look for resources in the hosting application which either apply the resources to their respective properties, or style out the control via hard coded defaults.
Is it possible to set up resource dictionaries within the project containing the UserControls so they can use those references as the default, instead of hard coding? If so, how do I target them?
(I have a ResourceDictionary set up within the same project as the controls: Resources>Dictionaries>Colors.xaml)
Thanks in advance!
E
You should really look at creating custom templated controls in library rather than derivatives of UserControls. This will allow projects that reference your library to specify an alternative default style for you controls in the same way as we can for the controls in Microsofts own SDK.
The Creating a New Control by Creating a ControlTemplate topic on MSDN is good starter.
I think this is a better explanation, but i'm trying on a desktop application and i got the same problem.
XamlParseException: Failed to create a 'System.Type' from the text 'local:CustomerEntity'
If I'm undestanding correctly you want to create the file "generic.xaml" in the folder "Themes". However, I don't believe automatic styling works with UserControl only with Control. Generally if you trying to make a control that can be stylized and retemplated you want to inherit from Control and not UserControl.

Resources