PRISM Sharing styles between modules - silverlight

I'm testing the reference project that karl has made for silverlight.
In it on of the modules have a StaticResource binding to a resource that is in the App.xaml file.
When i do the same the design view does not show the style in my module.
What am i missing?
Common contains Styles.xaml
Shell merges Styles.xaml
ModuleA {StaticResource StyleA}

This is a problem of the design tool. I suppose that the assembly of ModuleA is not referenced by the Shell, right? Then the tool has no reason to know that ModuleA will be loaded only when the Shell is loaded. I doesn't know that these projects are related, hence it doesn't look for styles in the Shell project when it loads a view from ModuleA in the designer.
One solution would be to reference the shared styles dictionary in every view of ModuleA that you want to open in the designer, but this is not a very good solution as it is a code duplication. Another solution, if you are using Expression Blend, might be to use design-time dictionaries, but I don't know if there is something like this for Visual Studio designer.

I can across the same problem. I've seen some Prism applications a "Common" project to define things like "Region Names" constants and etc. If the all the styles were defined there that might be a better solution. That way each module could reference these styles or create thier module specific styes.

Related

How to share ResourceDictionaries in composite WPF Application?

Have build composite WPF App with Unity Ioc (without Prism).
All my resources (Styles, Templates, Pics) stored in one module (Infrastructure) and merged in Shell-module's App.xaml.
In other modules I use it as StaticResource. Everything works fine when I start the project, but in VisualStudio Designer I can't see my Styles: 'the resource xyz could not be resolved'.
How should I configure VS to see all resources also in designtime?
Possible Solutions:
I could merge everything in each module, but then each resource would be loaded multiple. I could also write my own ResourceDictionary implementation (like: here), but I used to merge resources anyway everywhere - I would avoid this.
I have found a Prism reference solution for a composite application. I don't know how, but there it works without custom ResourceDictionary and without merging it in each module. VS Designer shows everything, although XAML Editor still says "the resource xyz could not be resolved"
Question: is it a feature of Prism? Or there is some configuration hidden?
Solution is simple (has nothing with Prism to do).
All resources should be defined in Shell module (not in Infrastructure module).
Then all modules should be referenced in Shell module project.
No need to merge resources in each module.
Each module knows only high level modules, and only shell knows everyone.

Where should I define my datatemplates?

I'm trying to work out the best way to couple my Views and ViewModels in MVVM and I've settled on the ViewModel-first approach using typed DataTemplates, as described in this article and in this answer. I'm using Prism and have multiple modules which have their own projects/class libraries.
My question is: Where should my DataTemplates live in my solution?
Should I put the DataTemplates in a Resource Dictionary which lives in the same project that has the types/ViewModels it renders?
Should I put the DataTemplates in a Resource Dictionary which lives in the project which has the application's MainWindow (i.e. Shell.xaml)?
Should these Resource Dictionaries then be added to App.Current.MainWindow.Resources.MergedDictionaries?
I hope this is enough information to describe what I'm trying to do.
Update: see comments of selected answer.
I'm sure that the best way here is to use Themes\Generic.xaml resources file. This is file (it should be exactly in folder Themes and has name exactly Generic.xaml) used by WPF/Silverlight themes engine and contains resources shared through whole application. You can also create separate file in folder Themes with name like Generic.DataTemplates.xaml and add link to it from Generic.xaml. Google knows a lot about generic.xaml or you can see more details in my answer here: WPF Prism - Where to put Resources?
Updated to explain more clear.
I will say if your DataTemplate is generic:
i.e You have a UserControl that binds to a ViewModel, and that ViewModel has BaseViewModel, which expose some sort of properties. Your DataTemplate is displaying those properties. So you can use this DataTemplate on every ViewModel that Implement the BaseViewModel.
Is better to put it in App.xaml, so you will able to pull it out with the Key and apply on different place in your project.
But if your DataTemplate is very specific,
i.e There is a UserControl that only binds on the specified property in that ViewModel and you know no other control will binds to that ViewModel, you will want to put into the same Xaml file's Resources or where you define your UserControl.
According to Microsofts App Studio the DataTemplates should live in a DataTemplates Subdirectory under the Views Directory. A Universal app has this directory for both the Windows UI as for the Windows Phone UI so its not in the Shared project because they are not the Same. Don't use the Converge PRISM architecture. Its completely wrong designed! That was not written with a Windows and a Windows Phone architecture in mind but like they call it Converged. It should have been completely redesigned like it works in Microsofts App Studio. Don't look for Dependency Injection its not in it and not needed. Most use Dependency Injection for stub or fake interfaces. The DataContext for design data works now so good with json data that a Dependency Injection component would be overkill.

Applying global styles to inherited Windows controls in control library

I'm a complete n00b to WPF but I'm working on my first application. I already realize that styles I use in the application I will likely want to use in future applications, so I'd like to use some method of applying global styles from project to project.
I've seen plenty of tutorials on creating a control library project, but they all go into creating custom controls. I don't really need custom controls (yet) per se, just the standard Windows controls with custom styles.
I'm also a little unclear on the whole ResourceDictionary thing. I've found examples on creating one for an application project, but not so much for a control library project.
What I'm looking for here is a) is a control library really what I need or am I creating more work than necessary? b) am I on the right path with a ResourceDictionary? and c) any good links to tutorials/examples that might go into what I'm trying to do rather than just a custom control creation tutorial.
You definitely want a ResourceDictionary with styles that you will be using in other apps. You can then reference it on an application, window, or even control level by including it in the application, window or control resources.
Where you put that resource dictionary isn't that important, though a custom control project is a common place to do so. It can be anywhere, in any project, and you can reference it with a URI.
Microsoft has a pretty good writeup on resources: Using Resources. Here's a decent tutorial on using dictionaries: Resource Dictionaries
You are in the right direction with ResourceDictionary. Create one for your application in a separate library move all your Styles there and refer them using Pack URI syntax. Here is a related question: ResourceDictionary in a separate assembly

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.

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.

Resources