End-to-end styles in the WPF class library

I have a C # class library assembly (2008 / .NET 3.5) that supports WPF (based on this article ).
I created several windows, and now I'm trying to create a common set of styles for them. However, since it is a class library (instead of a WPF application), I do not have app.xaml (and the application contained in it and the corresponding Application.Resources) that stores these styles for global access.

So: How to create a set of top-level style definitions that will be displayed by all xaml files in the assembly, given that I do not have app.xaml (see above)? And / or can I add a working app.xaml application to the class library?

FYI, I tried to create a ResourceDictionary in the ResourceDictionary.xaml file and include it in every window in the Window.Resources block. This turned out to solve the style of buttons, etc., but not for the closing window. I can put Style="{StaticResource MyWindowStyle}" in the window opener and it compiles and displays in the VS Design window perfectly, but during the actual execution I get a parsing exception (MyWindowStyle cannot be found, I assume that Visual Studio sees the dictionary included after the line in question, but the CRL does things more consistently and therefore has not yet loaded the ResourceDictionary).




Thanks for the ideas, but still not ... it is obvious that the class library does NOT support the use of generic.xaml implicitly. I added generic.xaml to my class library project and set its build action to Resource. It contains:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="{x:Type Window}" x:Key="MyWindow"> <Setter Property="Background" Value="Black"/> </Style> </ResourceDictionary> 

The xaml window that I want to use is as follows:

 <Window x:Class="MyAssembly.ConfigureGenericButtons" x:ClassModifier="internal" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Style="{StaticResource MyWindow}" Title="ConfigureGenericButtons"> ...Buttons, etc... </Window> 

Although the VS Design window does not display the window using the MyWindow style (i.e. black background), it compiles and runs. However, when an application containing this class library calls a call to display this window, I get a XamlParseException:

Unable to find resource named '{MyWindow}'.

I also tried to leave the Style parameter to see if the window would use the default style (and I tried this with x: Key in generic.xaml, both included and without it). There are no errors, but nothing defined in generic.xaml is also not displayed.

I'm doing something wrong here or any other ideas on how to use regular custom styles in a window (i.e. don't need to define styles in every xaml window) - with the caveat that this is NOT an application?

+43
c # wpf xaml
Dec 31 '09 at 20:59
source share
6 answers

Try to add

 Style={DynamicResource MyStyle} 

You cannot use StaticResource in this case.

+13
Jan 02 '09 at 18:00
source share

It sounds like a theme job.

  • Add /themes/generic.xaml ResourceDictionary to your project.
  • Add the following to AssemblyInfo.cs: [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
  • Profit!

All resources that you add to the shared will be used by all controls. You can also create profiled themes (Luna, Aero, etc.), Including a ResourceDictionary file with the correct theme name in the themes directory.

Here is a link to more information: Create and apply your own themes

+12
Jan 01 '09 at 4:00
source share

If you do not have app.xaml, you can load it into application level resources, but for this you need to write code (not xaml) similar to this ...

 void LoadIt() { ResourceDictionary MyResourceDictionary = new ResourceDictionary(); MyResourceDictionary.Source = new Uri("MyResources.xaml", UriKind.Relative); App.Current.Resources.MergedDictionaries.Add( MyResourceDictionary ) } 

check out this site: http://ascendedguard.com/2007/08/one-of-nice-features-about-wpf-is-how.html

+3
Jan 02 '09 at 15:38
source share

R. WPF (or the person formerly known as Dr. WPF) has an excellent article on this subject.

Here is an excerpt from the message where they create the application object and add resources:

 if (Application.Current == null) { // create the Application object new Application(); // merge in your application resources Application.Current.Resources.MergedDictionaries.Add( Application.LoadComponent( new Uri("MyLibrary;component/Resources/MyResourceDictionary.xaml", UriKind.Relative)) as ResourceDictionary); } 

Since my assembly is hosted via interop, I had to add the ShutdownMode setting as follows and exit:

 new Application() { ShutdownMode = ShutdownMode.OnExplicitShutdown }; 

It worked like a charm.

+1
Oct 08 '14 at 16:26
source share

if you load it into Window.Resource, the dictionary is available only to children of this window. You need to have him for the window and his children.

Try loading it into the app.xaml file. This should make it an application-level resource, not a window-level resource.

0
Dec 31 '09 at 21:45
source share

Therefore, having spent a lot of time, I finally realized this. Here's how:

  • In the WPF management library, add a new folder called themes .
  • Inside the themes folder, add a resource dictionary named generic.xaml .
  • Inside generic.xaml add your resource using the following syntax:

     <SolidColorBrush x:Key="{ComponentResourceKey {x:Type local:UserControl1}, MyEllipseBrush}" Color="Blue" /> 
  • In your control, use the following syntax to access this resource:

     Background="{StaticResource {ComponentResourceKey {x:Type local:UserControl1}, MyEllipseBrush}}" 

Notes:

  • Steps 1 and 2 are usually performed for Visual Studio when creating a new WPF control library.
  • I do not fully understand the purpose of the first parameter of ComponentResourceKey , but it is necessary. Use the name of the control that will use this resource.
  • The designer of Visual Studio could not find the resource in my case. This might be a cache problem, I'm not sure. However, at runtime it works neatly.
  • Read more about this syntax in this MSDN article .

Hope this makes life easier.

0
Mar 17 '17 at 3:53 on
source share



All Articles