I've a WPF application, with Prism, we are using some modules(and more globally, a lot of micro-services).
I'm searching for the best practices to provide to the application a template that could be used to represent a model.
Since I've to do this from a module, I cannot just create a ResourcesDictionary and add it to the App resources.
How would you do this? Our objective is to have a good isolation of features.
I think you've not really fully explained your purpose or what you have here. With that proviso in mind.
If you created a regular resource dictionary in an app you can merge that in app.xaml.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
And you can then target resources in there in markup using x:Key or implicit styling. And templating which targets a datatype.
What that does is add the things in your resource dictionary to a sort of dictionary. ( It's not exactly a dictionary ) It has key and value.
That has scope of your entire application.
You can reference the things in there and switch them out. You can merge another resource dictionary in code. Once they're in there though, they are there to stay until you shut the app down or clear those resources.
You can read a resource dictionary:
ResourceDictionary rd = new ResourceDictionary
{
Source = new Uri("Dictionary1.xaml", UriKind.Relative)
};
And merge it:
Application.Current.Resources.MergedDictionaries.Add(rd);
If you merge a resource dictionary in markup, it doesn't even have to be compiled. I never tried that in code but I guess you might well find you could merge a "loose" uncompiled resource dictionary. If it didn't work directly you could definitely xamlreader.Load or .Parse an uncompiled rd into one in memory.
That adds to application scope. If you wanted that then maybe you ought to just merge your resource dictionaries in app.xaml though.
If you want scope, then windows, usercontrols etc etc all have resources. You can merge a resource dictionary in at pretty much any level you fancy and it'll then have a narrower scope than application.
Related
<Application x:Class="CustControls.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="/ControlLibrary;component/Styles/ControlResource.xaml"/>
<ResourceDictionary Source="StringLocalization/Dictionary_fr-FR.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now i want change the ResourceDictionary's source name Source="StringLocalization/Dictionary_fr-FR.xaml" to Source="StringLocalization/Dictionary_en-US.xaml"
What should i do for that.
The MSDN-documentation explains,
"In code, you do not set the Source property. Instead, you must obtain
a ResourceDictionary object by either creating one or loading one. One
way to load an existing ResourceDictionary to call XamlReader.Load on
an existing XAML file stream that has a ResourceDictionary root, then
casting the XamlReader.Load return value to ResourceDictionary."
It looks like you can only obtain a ResourceDictionary by either creating one or loading one.
It's also important to understand your purpose with the use of ResourceDictionaries. If you are meant to use them as a 'shared resource' you cannot build the dictionaries using 'Embedded Resource' action. Make sure they are marked as 'Content' and linked properly to their path-locations. Furthermore, its also important to understand how Merged Dictionaries behave in regards to which resource is chosen over the other (taken from the MSDN-documentation):
Resources in a merged dictionary occupy a location in the resource
lookup scope that is just after the scope of the main resource
dictionary they are merged into. Although a resource key must be
unique within any individual dictionary, a key can exist multiple
times in a set of merged dictionaries. In this case, the resource that
is returned will come from the last dictionary found sequentially in
the MergedDictionaries collection. If the MergedDictionaries
collection was defined in XAML, then the order of the merged
dictionaries in the collection is the order of the elements as
provided in the markup. If a key is defined in the primary dictionary
and also in a dictionary that was merged, then the resource that is
returned will come from the primary dictionary. These scoping rules
apply equally for both static resource references and dynamic resource
references.
Looking at your code, it seems that you just want to load another ResourceDictionary into your application. If that's all you want then probably adding it to the MergedDictionaries collection may already suffice.
If you want to load one on runtime you can use the following code (or similar to). Just make sure that you don't embed your resources:
try
{
string path = #".\Themes\Dictionary1.xaml";
var xmlTextReader = new XmlTextReader(path);
var resourceDictionary = (ResourceDictionary)XamlReader.Load(xmlTextReader);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
Here's the code in case you need it. Let me know if this helps.
I'm having a little problem with Merging dictionaries in my WP7 application, The app contains a lot of pages and the loading time exceeded the market place limit which is 5 seconds, I had to put the majority of the pages and UI stuff in a separate class library to cut off the loading time, the problem is that the pages ware referencing static resources in the global app.xaml, I tried to move the xaml stuff in App.xaml to a separate resource dictionary in the class library project. I also referenced that resource dictionary file in every page.
<phone:PhoneApplicationPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
<Converters:TimeSpanToWeekAndDayConverter x:Key="weekAndDayConverter"/>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
The problem is that I always receive this exception :
Failed to assign to property 'System.Windows.ResourceDictionary.Source'.
I also tried to change the build action of that resource dictionary from Page to Resource to Content (With all their specific path syntax considerations) without any luck.
I hope someone has a idea.
Thanks
I don't think you need to move the resources - you can keep them in the App resources. It should work.
You need to reference the dictionary by using the full name. In this case:
Source="/AssemblyName;component/Resources.xaml"
Otherwise, it won't know how to find it.
I have a ResourceDictionary such as (MyResourceDictionary):
<ResourceDictionary xmlns
.....
>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SeriesTwo.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="SeriesDetailedInformation">
<StackPanel>
......content...
</StackPanel>
</DataTemplate>
</ResourceDictionary>
SeriesTwo.xaml looks like this and also has the DataTemplate with the same name
<ResourceDictionary xmlns=
.....
>
<DataTemplate x:Key="SeriesDetailedInformation">
<DataGrid>
......content...
</DataGrid>
</DataTemplate>
</ResourceDictionary>
On my view page, which SeriesDetailedInformation data template gets used? Does it depend on which resource dictionary I reference first on my page?
Any good links and other reading material regarding this topic is also appreciated.
A couple of things from this page seem relevant to your question:
Lookup behavior within the MergedDictionaries collection searches the
last-added ResourceDictionary first, and the search stops as soon as a
requested key is found. In other words, the retrieval logic from the
collection of merged resource dictionaries is last in, first out.
And:
Resources in a merged dictionary occupy a location in the resource
lookup scope that is just after the scope of the main resource
dictionary they are merged into.
From this I gather that the DataTemplate defined in your main dictionary would be used first, then SeriesTwo, and any referenced before/above SeriesTwo after that.
Also, why not just try it out and see what happens? I maintain a solution I call "DumbCrapTestApp" where I test stuff like this when I'm curious about how something works. It has a console app for when I just need to test something within the language, and a WPF and a Silverlight app for when I want to try something there. If I needed a WinForms app, I'd just add one and do my thing there. I simply put whatever code I want to test into the relevant app, set it as my Startup Project, and away I go. It's really useful for figuring out these little things and I've even used it to get clarification and verify answers before I post them here on SO.
I'm currently working on having dictionaries of styles and templates that I can dynamically apply to my application. Before this "new wanted" dynamical behavior, I had several resource dictionaries, one for each styled control, that I merged in the App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsDictionary.xaml"/>
<ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now, I'd like my application to be styled, so I decided to merge all my previous resources into a new one called "MyFirstTemplates" and to add only this dictionary to the App.xaml.
New dictionary "MyFirstTemplates.xaml":
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">"
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorsDictionary.xaml"/>
<ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
New App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyFirstTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Window}"/>
</ResourceDictionary>
</Application.Resources>
Note: The default style for the Window is to correct a bug of WPF 4, see Adding a Merged Dictionary to a Merged Dictionary
Now that I have made this change, I cannot use a color resource from "ColorsDictionary.xaml" as a StaticResource in "ControlsTemplateDictionary.xaml" anymore. If I change back to merging these files in the app.xaml, everything works. To make it work, I have to change these StaticResource for DynamicResource. Do you have any idea why this doesn't work anymore?
Thank you :-)
By moving the dictionaries out of App.xaml the resources from each dictionary aren't in the other's resource tree during loading of MyFirstTemplates.xaml. Your original setup first loaded ColorsDictionary which was then available through App resources to ControlsTemplatesDictionary while it loaded. In your new setup, in order for the color resource to be available in App resources it needs to be loaded through MyFirstTemplates, which in turn requires loading of both dictionaries, which in turn requires access to the color resource... so it's sort of an infinite loop of references that can't be resolved statically. DynamicResource can wait until everything is loaded and then access the color without issue.
To fix either use Dynamic or merge ColorsDictionary directly into ControlsTemplatesDictionary.
Great answer by John explaining why this is happening.
So the problem is that when using merged dictionaries within a merged dictionary, the inner dictionaries can't "use" each other as StaticResource.
Basic solutions:
Use DynamicResource
Use just a single level of hierarchy from App.xaml when using StaticResource
Both of these solutions have problems. DynamicResource has a performance problem. The 2nd solution limits you on how you organize your XAML resources.
Alternative solution:
I created a small simple program (provided below in GitHub) that will run as a pre-build event and merge XAML files from a folder into one long .XAML file. Well, they need to be with a different extension (.txaml), otherwise they will be compiled.
This allows to structure resources folders and files however you want, without WPF’s limitations. StaticResource and the designer will work always.
The code in GitHub contains a simple solution that contains the merging program. It merges 2 folders into 2 files. One for App.xaml resources and the other for Generic.xaml resources. The .xaml files in a "Controls" project (There's also "Main" project).
Blog post explaining this
Consider a File | New Project of a WPF Application that contains:
A new custom control named CustomControl1
Two new resource dictionaries named Dictionary1 and Dictionary2
Take the generated style out of Generic.xaml and move it to Dictionary2. Then merge Dictionary2 into Dictionary1 and Dictionary1 into Generic like this:
<!--Generic.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Themes/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--Dictionary1.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
Then, add an instance of CustomControl1 into MainWindow's grid. (This part is necessary to reproduce the issue. The project always compiles fine - only at runtime does the issue show up, and the dictionaries must be referenced.)
In Dictionary1.xaml I am merging in another dict in the same folder, so a simple Source="Dictionary2.xaml" works. Yet in Generic.xaml I must use an absolute URI. If I change the above to be Source="Dictionary1.xaml" without the pack://application stuff then I get a XamlParseException caused by an IOException "Cannot locate resource 'dictionary1.xaml'" when it tries to construct the MainWindow.
My Question: What's special about generic.xaml regarding relative URI resolution, and why?
Excuse me because I have no ability to write comments so I post this as an answer.
I have the same situation and everything works fine for me. I don't need to put "pack://application" in the path in Generic.xaml. But only when the output type of an assembly is "Windows Application".
For "Class library" I need to add assembly name to the path (Source="/ClassLibarayAssemblyName;component/Themes/Dictionary1.xaml") becasue without it WPF engine tries to look for Dictionary1.xaml in application's main assembly.
Target framework in both cases is ".NET Framework 4 Client Profile"
Just a guess: generic.xaml needs to be accessible from outside assemblies as well, so it's a way to ensure that the resources can be found from anywhere, using absolute URIs. As I said, it's just a stab in the dark, not sure.