繁体   English   中英

具有自定义控件属性的Xaml ThemeResource imagebrush绑定

[英]Xaml ThemeResource imagebrush binding with custom control property

我目前正在使用我使用主题资源字典的Windows UWP应用程序。

我的主题字典中有

<ResourceDictionary.ThemeDictionaries>
    <!-- Light Theme resources -->
    <ResourceDictionary x:Key="Light">
        ...
        <ImageBrush x:Key="Hamburger" ImageSource="Assets/Images/DarkHamburger.png"/>
        ...
    </ResourceDictionary>
    <!-- Dark Theme resources -->
    <ResourceDictionary x:Key="Dark">
        ...
        <ImageBrush x:Key="Hamburger" ImageSource="Assets/Images/LightHamburger.png"/>
        ...
</ResourceDictionary.ThemeDictionaries>

和自定义控件属性

public ImageBrush toggleButtonImage
    {
        get { return base.GetValue(toggleButtonImageProperty) as ImageBrush; }
        set { base.SetValue(toggleButtonImageProperty, value); }
    }
    public static readonly DependencyProperty toggleButtonImageProperty =
        DependencyProperty.Register("toggleButtonImage", typeof(ImageBrush), typeof(HomeHeaderControl), null);

最后,如果我写这个,它就可以了

<HomeHeaderControl.toggleButtonImage>
     <ImageBrush  Stretch="UniformToFill" ImageSource="Assets/Images/LightHamburger.png"/>
</HomeHeaderControl.toggleButtonImage>

但是主题资源绑定不起作用

toggleButtonImage="{ThemeResource Hamburger}"

除ImageBrush之外,其他绑定均有效。 任何帮助将不胜感激。

您将必须使用绝对路径和位图图像

<ResourceDictionary.ThemeDictionaries>
<!-- Light Theme resources -->
<ResourceDictionary x:Key="Light">
    ...
    <BitmapImage x:Key="Hamburger" ImageSource="ms-appx:///Assets/Images/DarkHamburger.png"/>
    ...
</ResourceDictionary>
<!-- Dark Theme resources -->
<ResourceDictionary x:Key="Dark">
    ...
    <BitmapImage x:Key="Hamburger" ImageSource="ms-appx:///Assets/Images/LightHamburger.png"/>
    ...

谢谢大家,我发现了失踪者。 我在ImageSource中使用相对路径。

我使用-> <ImageBrush x:Key="Hamburger" ImageSource="ms-appx:///Assets/Images/DarkHamburger.png"/>并对其进行了修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM