在基页中声明的自定义主题控件
Custom themed controls declared in base page
我有一些样式文件。
例如 ButtonStyles.xaml 看起来像这样:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style
x:Key="CustomButtonStyle"
TargetType="Button">
<Setter
Property="Foreground"
Value="White" />
<Setter
Property="Background"
Value="Black" />
<Setter
Property="Height"
Value="32" />
</Style>
<Style
TargetType="Button"
BasedOn="{StaticResource CustomButtonStyle}">
</Style>
</ResourceDictionary>
而且我有从我的 BasePage 延伸出来的页面。
如果我想让我的新样式覆盖控件,我必须在每个页面中添加此代码:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
但是,如果我的 BasePage 中有此代码,它不会为我的控件设置主题。
有没有办法在每个页面中导入我的样式而无需一遍又一遍地复制粘贴此代码?
我无法在我这边使用 BasePage
重现此问题。如果您能 post 一个可以重现您的问题的示例就更好了。
但是,要在每个页面中使用自定义样式,我建议将资源字典合并到 Application.Resources
中。 Application.Resources
是放置应用导航结构中多个页面引用的任何应用特定资源的最佳位置。如果在 Page.Resources
中找不到请求的资源,XAML 资源系统将尝试检查 Application.Resources
属性。因此,如果我们将资源字典合并到 Application.Resources
,我们就可以在应用程序的所有页面中使用它们。而如果我们想在某些页面使用一些不同的样式,我们可以在它们的Page.Resources
中指定新的Style
。有关详细信息,请参阅 Lookup behavior for XAML resource references。
因此我们可以将资源字典合并到 Application.Resources
中,如下所示:
(注意../Styles/ButtonStyles.xaml
不能在WinRT中使用XAML,我们应该用“/”来指代包根。)
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
那么在其他页面中,如果我们不为Button
指定Style
,就会使用ButtonStyles.xaml中定义的样式。
我有一些样式文件。
例如 ButtonStyles.xaml 看起来像这样:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style
x:Key="CustomButtonStyle"
TargetType="Button">
<Setter
Property="Foreground"
Value="White" />
<Setter
Property="Background"
Value="Black" />
<Setter
Property="Height"
Value="32" />
</Style>
<Style
TargetType="Button"
BasedOn="{StaticResource CustomButtonStyle}">
</Style>
</ResourceDictionary>
而且我有从我的 BasePage 延伸出来的页面。 如果我想让我的新样式覆盖控件,我必须在每个页面中添加此代码:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
但是,如果我的 BasePage 中有此代码,它不会为我的控件设置主题。
有没有办法在每个页面中导入我的样式而无需一遍又一遍地复制粘贴此代码?
我无法在我这边使用 BasePage
重现此问题。如果您能 post 一个可以重现您的问题的示例就更好了。
但是,要在每个页面中使用自定义样式,我建议将资源字典合并到 Application.Resources
中。 Application.Resources
是放置应用导航结构中多个页面引用的任何应用特定资源的最佳位置。如果在 Page.Resources
中找不到请求的资源,XAML 资源系统将尝试检查 Application.Resources
属性。因此,如果我们将资源字典合并到 Application.Resources
,我们就可以在应用程序的所有页面中使用它们。而如果我们想在某些页面使用一些不同的样式,我们可以在它们的Page.Resources
中指定新的Style
。有关详细信息,请参阅 Lookup behavior for XAML resource references。
因此我们可以将资源字典合并到 Application.Resources
中,如下所示:
(注意../Styles/ButtonStyles.xaml
不能在WinRT中使用XAML,我们应该用“/”来指代包根。)
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
那么在其他页面中,如果我们不为Button
指定Style
,就会使用ButtonStyles.xaml中定义的样式。