I tried to add all my resources to a resource template file, and incorporate them as dynamic resources (precisely the way Blend does this too). The programme compiles and runs fine, but the styles aren't applied at all. Bizarrely, they are applied in the preview, though. This is the code I have:
// MyUserControl.xaml
<UserControl x:Class="MyProject.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
d:DesignHeight="411" d:DesignWidth="645">
<Grid>
<Button Content="Click here" Style="{DynamicResource MyButtonStyle}" />
</Grid>
</UserControl>
-
// MyStyleTemplates.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FF263B5B" Offset="0"/>
<GradientStop Color="#FF65FD43" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
-
// App.xaml
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProject;component/MyStyleTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
I tried to do exactly the same but then putting the Style
code within the <UserControl.Resources>
tag in the MyUserControl.xaml
file, and that worked fine again, so nothing is wrong with the Style itself. Why does this happen?
Edit:
As this may have something to do with it, the folder structure may be slightly unconventional, as follows:
MyProjectFolder /
MyProject.csproj
MyStyleTemplates.xaml
Main /
App.xaml
GUI /
MyUserControl.xaml
MyUserControl.xaml.cs
Is this allowed?