绑定到ResourceDictionary中的静态主题

本文关键字:静态 ResourceDictionary 绑定 | 更新日期: 2023-09-27 17:50:07

我正在尝试为WP8应用程序创建一个自定义主题。不是内置的暗/光主题,而是自定义白标主题。为了使它工作,我创建了一个静态类:

public class Theme : DependencyObject
{
    static Theme()
    {
        CurrentTheme = new DefaultAppTheme();
    }
    public static IAppTheme CurrentTheme { get; set; }
}
然后,我可以通过首先在Resource部分包含引用来绑定到页面中的静态属性:
<pages:PhoneApplicationPage.Resources>
    <ResourceDictionary>
        <ui:Theme x:Key="Theme"/>
    </ResourceDictionary>
</pages:PhoneApplicationPage.Resources>

最后:

<Rectangle Fill="{Binding Path=CurrentTheme.DarkIconBrush, Source={StaticResource Theme}, Mode=OneTime}">

到目前为止一切顺利。唯一剩下的就是在/Themes/Generic中做同样的事情。xaml文件,但是我怎么也弄不清楚。

我可以通过运行应用程序来验证上面的绑定是否有效。通过设置主题。当前主题在应用程序中,我可以改变应用程序的主题。

如何在泛型中绑定。xaml到我的主题类的颜色属性?这是我尝试过的:

  1. 在Generic中的ResourceDictionary中添加Resources部分。xaml -不工作,因为这个类不支持它

  2. 尝试在泛型中将其声明为变量(资源)。并引用它,如:

    x

    & lt; ui:主题:关键="MyTheme"/祝辞

不确定这里的绑定语法会是什么样子。

  • 做一些奇怪的附加属性绑定魔法来解决问题。
  • 通用。xaml在App项目中是而不是,它在User controls项目(库)中。否则,我可以将它添加到App类的"资源"部分。

    绑定到ResourceDictionary中的静态主题

    免责声明:我不是WP开发人员,我是为普通桌面浏览器Silverlight开发的。

    我的心理模型如何默认样式模板通用。xaml主题一起工作的方式如下(这可能也适用于您):

    为了让我的(模板化的)自定义控件在整个应用程序中正常工作,框架必须能够找到默认模板。因此,这些模板的默认样式包含在generic.xaml中。心智模型:通用。xaml不涉及主题化,它唯一关心的是为我的自定义类型提供模板(和默认设置),因此我的应用程序UI在技术上是可用的。

    这是我的责任,我的默认模板实际上绑定并使用与控件外观相关的公共依赖属性:背景,BorderBrush, BorderThickness,前景,填充

    也许我需要自己添加一两个属性来允许我的控件自定义:

    [StyleTypedProperty( Property = "ToggleButtonStyle", StyleTargetType = typeof( ToggleButton ) )]
    [StyleTypedProperty( Property = "DropDownBackgroundStyle", StyleTargetType = typeof( Border ) )]
    public class DropDownButton : Control
    {
        ...
        public Style ToggleButtonStyle
        {
            get { return (Style) GetValue( ToggleButtonStyleProperty ); }
            set { SetValue( ToggleButtonStyleProperty, value ); }
        }
        public static readonly DependencyProperty ToggleButtonStyleProperty =
            DependencyProperty.Register( "ToggleButtonStyle", typeof( Style ), typeof( DropDownButton ), null );
        public Style DropDownBackgroundStyle
        {
            get { return (Style) GetValue( DropDownBackgroundStyleProperty ); }
            set { SetValue( DropDownBackgroundStyleProperty, value ); }
        }
        public static readonly DependencyProperty DropDownBackgroundStyleProperty =
            DependencyProperty.Register( "DropDownBackgroundStyle", typeof( Style ), typeof( DropDownButton ), null );
    }
    

    心理模型:可定制性/可定制性是我必须在自定义控件中内置的东西。

    如果在一个具体的应用程序(和不同的程序集)中,我需要我的控件遵循一个主题(不需要为每个事件显式地设置样式),我必须提供一个单独的默认样式,但是这个新的默认样式只设置那些与外观有关的属性:所以我要在app。xaml:

    中添加这样的内容
    <Application.Resources>
        <ResourceDictionary>
            <ui:Theme x:Key="Theme"/>
            <Style TargetType="ToggleButton" x:Key="ThemedToggleButtonStyle">
                <Setter Property="Background" Value="{Binding Path=CurrentTheme.DarkIconBrush, Source={StaticResource Theme}, Mode=OneTime}"/>
            </Style>
            <!-- implicity default style for all ToggleButtons -->
            <Style TargetType="ToggleButton" BasedOn="{StaticResource ThemedToggleButtonStyle}"/>
            <!-- implicity default style for all DropDownButtons -->
            <Style TargetType="controls:DropDownButton">
                <Setter Property="ToggleButtonStyle" Value="{StaticResource ThemedToggleButtonStyle}"/>
                <Setter Property="Background" Value="{Binding Path=CurrentTheme.DarkIconBrush, Source={StaticResource Theme}, Mode=OneTime}"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
    

    心理模型:对于一个主题应用在我的一个具体的应用程序,我必须"添加第二层"的隐式默认样式。这是我的具体应用程序所关心的,而不是我的custom-controls- assembly所关心的。