I'm currently trying to figure out how I can get the WPF designer to display correctly a list of resources I can use.
For example when using a TextBlock in xaml, I can "preview" the useable resources, for usual.
This does not seem to work well when using it from an external DLL/assembly (binary).
I've already tried to use the usual source strings for ResourceDictionary,
<ResourceDictionary Source="pack://application:,,,/MyAssembly;component/Themes/Generic.xaml"/>
<ResourceDictionary Source="/MyAssembly;component/Themes/Generic.xaml"/>
as mentioned in various other questions and answers while googling this issue, though not even the direct Uri to the required ResourceDictionary seems to work.
Is there any fix for this issue or does it simply not work ?
I don't want to disable the designer, please do not suggest so.
Related
So, I just switched to Windows 10 and I noticed all my desktop WPF applications are noticeably "uglier" than they where on Win7/Win8.
I know this sounds subjective/pointless, but let me be more precise. Here's some examples of objective degradation of the UI's usability/clarity:
Everything looks like Windows 95, all gray without shading of any kind, almost like there's a custom theme (or a default theme is missing or something)
A lot of disabled elements do not get "grayed out" (see image below)
Disabled buttons do not get flattened (again, see image)
ListView items are not highlighted on mouseOver
ProgressBars have the "blue segments" pre-XP look
etc.
Is this "normal", i.e: is this the new default WPF theme?
If so, can I change it? It's really bad
If not, what am I missing?
EDIT:
After testing on another machine, turns out the problem had nothing to do with WPF or Windows.
Just DON'T apply any tweaks for win10 you find on the internet to restore the coloring on windows borders. It messes up the WPF theme engine.
Sorry for wasting your time guys.
Well, I found out how to fix it (thanks to gix for pointing me in the right direction):
First step: reference the PresentationFramework.Aero2 assembly in your project
Second step: add the following to your App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero2;component/themes/Aero2.NormalColor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now it looks like it does in Windows 8. Note that you can also use Aero instead of Aero2 if you want the Windows 7 look (also change the referenced dll of course!), but I think Aero2 looks much more "native" on Win10.
EDIT:
After further testing, turns out this is not 100% perfect, although it's close enough. For example: disabled text boxes are not grayed out, and ListViews seems to not be affected at all, they still look like they crawled out of Windows 98's ass... if anyone has a clue on how to make the Aero2 style stick to them as well it would be great
I created a custom content control with its default template.
The template though is not located in the location "Themes\Generic.xaml" as it is supposed to be. Instead I placed it in a separate resource dictionary with a different name.
It is merged along with other resource dictionaries in the App.xaml resource dictionary.
Running the code yielded no errors and the control is visible. I tested the application on other machines and the code ran just fine , I would like to point out that these machines are development machines.
Testing the application on the client machines (non development) then revealed some strange behaviour. The custom control was not visible; it would only be visible on 1 out 3 client machines.
I was able to fix the problem by putting the template in "Themes\Generic.xaml".
My question is how was it able to work without having to put the template in "Themes\Generic.xaml" ? I have searched on Google but I could find any clear cut explaination .
Thank you
For your templates to be applied they need to be present in Themes\Generic.xaml.
The good news is that you may merge them here, which I find to be good practice, and makes your code cleaner. Often my generic.xaml consists of only this, one X.Generic.xaml for all your cust. controls.
Themes/Generic.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...>
<!-- Your custom controls split into separate files -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/YourNamespace;component/Themes/ControlA.Generic.xaml" />
<ResourceDictionary Source="/YourNamespace;component/Themes/ControlB.Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I have a windows 8.1 XAML application. I want to allow users to customize the theme of the app themselves - for example, I want to give them a color picker where they can set various colors in the app, which would set various resources used across my app.
The issue though, is I cant find out how to dynamically change the value of a resource. I know in 8.1 they added the concept of a theme resource, which allows me to change from light to dark theme at runtime and what not. But my issue is that I'd like to say 'the backgroundColor resource is now going to be orange, and all items using this resource will reflect this change'
I believe the DynamicResource XAML element is what I needed, but that seems to be from WPF and not supported in Win8. Does anyone have suggestions?
In short - I want to be able to easily provide dynamic resource brushes.
Unfortunately I do not believe that there is a nice built in way of doing this so any solution is likely to be a little bit hacky or unpleasant.
This question has some good answers of which this answer is my personal favourite. The downside is that it results in the ViewModel taking on a bit more responsibility for the view than is ideal.
You can actually dynamically access the attributes of the elements you wish to change upon the user's choice.
I'm going crazy with different XAML designer errors in my PRISM application.
I have custom controls inside Infrastructure project and it seems that all referencing projects have issue loading control template at some point.
Everything runs good at runtime but VS2010 just doesn't work with all kind of errors.
I know for sure it is related to custom control not properly loading style from generic.xaml for some reason.
What I want to try is to drop generic.xaml all together and switch to use named templates for my custom controls. This way I can always merge resource files so VS know where to look for stuff.
Do you think this is viable solution?
How do I populate DefaultStyleKey from named style?
This is not a viable solution: custom controls pull their information from Themes/generic.xaml. This is baked into the runtime.
If you're building a WPF/Silverlight composite application, you'll need to put your generic.xaml in your WPF project and link it to your Silverlight project. That's the reverse of Prism guidance, but is required due to a bug with linked XAML files that are not at the root level in WPF projects.
Without generic.xaml, it is some what difficult but with help of generic.xaml you can switch to use named templates for your custom controls. just simply adding following code in generic.xaml
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CPMS.Utility.Controls;component/Controls/MyUc/Style/DBLoginForm.xaml" />
</ResourceDictionary.MergedDictionaries>
I have the following:
MainApp
- Assembly 1 (Contains UserContolA)
- Assembly 2 (Contains UserControlB)
- Etc.
Not every Assembly will be loaded though, so
Neither assembly can compile if I try to assign resources to controls (button, combobox, etc.) that it can't find because I want my MainApp to dictate those resources. But, if I include the resources in the assemblies it seems that I can't override them (because WPF goes from bottom-up for resources giving prioroty to those closest to the bottom).
Also, I'm afraid even if I do solve this problem trying to get it to be dynamic in a way that a user can select from different "themes" that it'd make it even more difficult.
I included prism in the topic because I plan to use that as my framework, but haven't looked deep enough into it to see if it affects this in any way or has something built into it already, which would be a Godsave.
Really what you are asking is what is the best approach to splitting up a WPF application into multiple assemblies. Your strategy is to create reusable control libraries and let the app integrate everything. Great so far. The last issue you need to address is resources. To handle that you need to add a Themes\Generic.xaml and move all the resources in your library into it. Then the library can find all the resources it needs and the application can override it if it so wishes.
Here is a good article that discusses some of the issues related to resources across multiple assemblies:
Control Authoring Overview
Also be sure to set up your control assembly attributes so that Generic.xaml will be searched:
[assembly: ThemeInfo(
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly)]
Edit:
The above solution applies to custom controls, not to UserControl resources. To handle UserControl resources in a separate assembly that the main application can override, you can use this approach:
Use {DynamicResource ...} and simply leave the resource definition out of the UserControl.
The problem with this is that although the resource will be located correctly at run-time, it won't be found during design-time and this can make your UserControl very hard to work with. There are two workarounds for this problem:
Use Expression Blend together with its "Design Time Resources" feature. Simply opening a project that has resources that cannot be resolved will cause Blend to prompt you for which resources to use at design-time.
Use Visual Studio to design your UserControl and include design time resources in the XAML itself.
For Visual Studio you can include this in your UserControl while designing it only:
<UserControl.Resources>
<ResourceDictionary Source="/YourControlLibrary;component/DesignTimeResources.xaml"/>
</UserControl.Resources>
Once you are done designing it, simply comment out that code in the XAML. It's inconvenient, but until Visual Studio supports design time resources, at least it works.