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?