Loading WPF Resources more than once - wpf

I am working in WPF and WinForms for both Windows XP and Windows 7/10 users (.NET 4.0 due to XP).
Some WPF Windows are used as dialogs for older WinForms applications without a WPF Application class/App.xaml file.
This in itself isn't a problem but I find I'm having to declare styles in resource dictionaries in user controls/windows in the WinForms/WPF situations which isn't normally necessary in plain WPF applications due to App.xaml.
My question is whether WPF detects that the same resource is being loaded twice (in a pure WPF application e.g. UserControl and App.xaml) and copes with/manages this without interference from me or do I have to try to ensure I only declare resource dictionaries once?
Many thanks.

The answer is no.
If you load a resource dictionary as a resource for a usercontrol then each instance of the usercontrol means another instance of any resources it uses in memory.
If that is going to be a problem then you need to do something.
You might just be able to instantiate an application object and use that to stash your resources in. Application.Current.Resources is after all just referencing the current application. Depends on exactly how your app works.

If you try to load the same resource dictionary twice, the application will throw a runtime error.
But, if you are only defining the styles already present in the resource dictionary, in the user control also, i.e, basically two styles with the same name one in the resource dictionary and the other in the user control, then no error is thrown. The style in the user control will have a higher priority.

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: 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

Programmatically get XAML of an FrameworkElement in SL4

I tried to serialize a FrameworkElement with DavidPoll's XamlSerializer, but I get a totally different result than the (right) one that I get with Silverlight Spy. For example with XamlSerializer I get alot of empty tags(like MatrixTransform, ScrollViewer etc.) but not the ContentPresenter node that I get with Silverlight Spy. Any idea how to get a clean logical tree at runtime?
Update: I have a form designer program that stores those forms in a DB and at runtime it creates objects (user controls) from that info stored in a DB. Each form is composed of some fields. At runtime it programmatically creats an UserControl that contains all fields. I would like to get the generated XAML, just like I can get it with Silverlight Spy.
edit: I'm not limited to Silverlight environment, a soltion for a desktop application is valid too.
Short answer is: I don't think you can from within Silverlight itself. Sorry.
Once the XAML has been parsed and loaded into framework elements you have already lost information. For instance regenerating a path from the in memory object is non-trivial and you won't have access to the internals of the path element from Silverlight code.
Silverlight Spy adds back information that a simple XamlSerializer won't generate. Silverlight Spy has no limitations as it is a "real" desktop application, with full operating system access, whereas you want to actually do this in Silverlight itself which is very restricted sandbox environment (for security reasons).

WPF: What is App.xaml's Purpose?

I've done .Net development for awhile but I'm new to the WPF technology. What is the supposed purpose of App.xaml? Also, what type of xaml code do you usually put in it? It seems like for simple applications it could be ignored and left untouched. Is this true?
App.xaml is the declarative portion of your code (usually generated by Visual Studio) extending System.Windows.Application. For example, Expression Blend can use App.xaml to share a Resource Dictionary or a design-time data set with your entire application. And, because we are using Microsoft products, whatever Expression Blend can do auto-magically, we can do by hand in Visual Studio.
Now the tangent: To me, to ask about the purpose of App.xaml is to ask about the purpose for System.Windows.Application. Feel free to accuse me of changing the original question (let the digital brutality ensue).
You can’t just open a System.Windows.Controls.Window in any Assembly you like… Chris Sells is likely telling me this in his book. I began to understand the purpose of System.Windows.Application while using MEF and MVVM Light to display WPF windows in DLLs (not EXEs). I got errors like this:
The type 'System.Windows.Markup.IComponentConnector' is defined in an assembly that is not referenced.
or
The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced.
The above error is simply saying that I’m trying to open a WPF Window inside of a DLL and not an EXE. Then, there’s this error:
The component 'Songhay.Wpf.WordWalkingStick.Views.ClientView' does not have a resource identified by the URI '/Songhay.Wpf.WordWalkingStick;component/views/clientview.xaml'.
This boils down to the absence of a facility that associates WPF Window XAML with the WPF “code” (an instance). This facility is associated with WPF EXEs and not WPF DLLs. Visual Studio auto-generates a WPF EXE class called App.g.cs (in your \obj\Debug folder) with this call in it: System.Windows.Application.LoadComponent(this, resourceLocater) where resourceLocater is a badly named variable containing a System.Uri pointing to the XAML like ClientView.xaml mentioned above.
I’m sure Chris Sells has a whole chapter written on how WPF depends on System.Windows.Application for its very life. It is my loss (quite literally of time) for not having read about it.
I have shown myself a little something with this unit test:
[STAThread]
[TestMethod]
public void ShouldOpenWindow()
{
Application app = new Application();
app.Run(new Window());
}
Failing to wrap a new Window in the System.Windows.Application.Run() method will throw an error from the land of COM talking about, “Why did you pull the rug from underneath me?”
For simple applications, it is true, it can be ignored. The major purpose for App.xaml is for holding resources (style, pens, brushes, etc.) that would would like to be available through out all of the windows in your application.
It is true. App.Xaml is some sort of central starting point. You CAN use it, or you CAN start your first window (it is defined in the app.xaml) manually. There are some lifetime events there centralls (like application start).
Storing resources that are used across the whole application.
Application is the root of the logical tree.
It is like Global.asax if you are coming from an ASP.NET background. You can also use it to share resources throughout your application. Comes in pretty handy for resource sharing.
App.xaml is a major part of wpf application.
It contains major four attributes
1.X:Class->used to connect you xaml and code-behind file(xaml.cs).
2.xmlns->To resolve wpf elements like canvas,stack panel(default one).
3.xmlns:x->To resolve XAML language definition.
4. StartupUri->To give start window when application is launching.
++++++++
App.xaml is the declarative starting point of your application. Visual
Studio will automatically create it for you when you start a new WPF
application, including a Code-behind file called App.xaml.cs. They
work much like for a Window, where the two files are partial classes,
working together to allow you to work in both markup (XAML) and
Code-behind.
App.xaml.cs extends the Application class, which is a central class in
a WPF Windows application. .NET will go to this class for starting
instructions and then start the desired Window or Page from there.
This is also the place to subscribe to important application events,
like application start, unhandled exceptions and so on.
One of the most commonly used features of the App.xaml file is to
define global resources that may be used and accessed from all over an
application, for instance global styles.
+++++++++
Source : http://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/
Here is an updated answer in case people are still looking.
There is this excellent article on WPF, and the link specifically puts you at the App.Xaml point to begin teaching you the things you can do with it.
WPF is easy for the first very simple app or two. However, due to the increased flexibility of the framework, you need these types of tutorials to help you understand what can be done from where (in the various application files).
https://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/
Good luck.

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