我尝试将我的所有资源添加到资源模板文件中,并将它们合并为动态资源(Blend 也是这样做的)。该程序编译并运行良好,但根本没有应用样式。奇怪的是,它们被应用在预览中。这是我的代码:
// 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>
我尝试做完全相同的事情,然后将Style
代码放在文件中的<UserControl.Resources>
标记中MyUserControl.xaml
,并且再次正常工作,因此样式本身没有任何问题。为什么会这样?
编辑:
由于这可能与它有关,因此文件夹结构可能有点不合常规,如下所示:
MyProjectFolder /
MyProject.csproj
MyStyleTemplates.xaml
Main /
App.xaml
GUI /
MyUserControl.xaml
MyUserControl.xaml.cs
这是允许的吗?