【问题标题】:How do I use the icons provided with the WP7 sdk properly?如何正确使用 WP7 sdk 提供的图标?
【发布时间】:2010-10-12 23:49:20
【问题描述】:

SDK 中提供了“浅色”或“深色”的图标,具体取决于手机上设置的主题。当主题更改时,应用程序栏上的图标会自动更改。此外,当您按下按钮时,无论您使用哪个主题,图像都会反转(因此它仍然可见)。我可以很容易地弄清楚如何根据当前主题更改图标。然而,当按钮被按下时如何改变图标并不容易。

为了更清楚。假设我正在使用“黑暗”主题。我创建了一个使用深色图标的按钮。按下按钮时,背景为白色,但图标本身保持白色,因此不可见。在应用程序栏上,图标将变为黑色,这在白色背景下当然可见。

这有意义吗?有谁知道如何解决这个问题?

【问题讨论】:

    标签: silverlight themes windows-phone-7


    【解决方案1】:

    您可以使用单个图标作为 OpacityMask,而不是使用浅色和深色图标。这只是一个示例,您可能希望将其制成一个单独的控件以使图标设置更整洁,如果需要,您可以为 ToggleButton 提供 C# 代码。 this series 也可能会引起一些兴趣。

    <Style x:Key="IconButton" TargetType="Button">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
            <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
            <Setter Property="Padding" Value="10,3,10,5"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                                <Grid x:Name="ContentContainer" OpacityMask="{TemplateBinding Content}" Background="{TemplateBinding Foreground}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    用法

    <Button Style="{StaticResource IconButton}" >
        <ImageBrush ImageSource="appbar.feature.search.rest.png" Stretch="None"/>
    </Button>
    

    说明:OpacityMask 使用元素画笔的 alpha 值,因为它需要画笔,您可以使用渐变或 ImageBrush。在这种情况下,矩形的 ContentContainer 采用所提供图像的形状,因为仅使用 alpha 通道,它可以是您想要的任何颜色。 ContentContainer 使用按下时样式更改的前景色,您可以通过更改按钮前景色来更改图标颜色。此样式基本上是默认样式,但使用带有 OpacityMask 的 Grid 代替 ContentControl。

    通常您不会将 ImageBrush 直接放在按钮中,但这是作为 Silverlight v3 中某些绑定限制的快速解决方法。或者,您可以使用具有 Uri 属性的自定义控件来更新蒙版的 Brush 属性。该样式将使用自定义 Brush 属性作为 OpacityMask 而不是 Button.Content。由于 OpacityMask 仅使用 alpha 通道,因此此方法不适用于彩色图像。

    【讨论】:

    • 病了第二个,不透明蒙版是要走的路,一开始有点混乱,但很值得理解
    • 我想我明白你的意思,但这个例子不起作用。它只是给了我一个全白的按钮。
    • 我用浅色和深色主题对其进行了测试,检查图像是否在您的项目中,并且您将 ImageBrush 添加到按钮而不是图像。
    • 修复它的是 ImageBrush。它有效,但我不确定为什么。你能给我一个很好的解释吗?我对 WPF/Silverlight 有相当深入的了解,但我以前从未使用过 OpacityMask。
    • 很高兴你能成功。我添加了对其工作原理的解释。
    【解决方案2】:

    实际上 - WP7 有一些聪明之处,如果满足以下条件,它会自动制作“深色”版本的浅色图标(并正确处理按钮按下):

    • 图标是 48x48
    • .PNG 格式(实际上没有尝试过使用 .jpg,但是 我怀疑它会起作用)
    • 图标在哪里 白色和背景是 透明(可能也适用于黑色,但您的问题似乎另有建议)

    我在 WP7 应用程序中使用了一组图标,它会自动处理浅色/深色主题 - 以前也有一个 AppBarIcon pack from Microsoft,但一个快速的谷歌目前只是给我断开的链接。如果您感到绝望,请给我发送电子邮件,我可以按您的方式轻弹它们。

    虽然建议的使用 OpacityMask 的方法可能有效,但听起来比仅在透明的 48x48 png 上使用白色要困难得多。 :) 让我知道你的情况!

    【讨论】:

    • 问题是,如果不按照建议手动执行不透明蒙版,它就不起作用。它唯一起作用的地方是应用程序栏。
    • 对 - 抱歉,我误解了你的问题 - 我以为你是在尝试获取一个应用栏图标,而不仅仅是一个在你的应用中通用的图标。很抱歉造成混乱!
    • 请注意 - 我在应用栏中使用了 LIGHT 主题的 SDK 按钮,但我的应用在第 5.5 点失败 - 使用深色主题,按钮内部是黑色的。如果您从 DARK 主题 SDK 按钮开始,那么它会根据您使用的主题正确地反转颜色...故事的寓意 - 在您的应用栏中使用 DARK 主题 SDK 按钮,而不是浅色的...
    • 很好的信息!我使用 76x76 透明 PNG(白色像素)并且效果很好(适用于应用程序栏、WP8 应用程序)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 2020-06-04
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多