SDK中已根据手机上设置的主题为“亮”或“暗”提供了图标。当主题更改时,在应用程序栏上,图标也会随之自动更改。同样,当您按下按钮时,无论您处于哪个主题,图像都将反转(因此仍可见)。我可以很容易地弄清楚如何根据当前主题更改图标。但是,不容易弄清楚的是如何在按下按钮时更改图标。

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

这有意义吗?有人知道怎么修这个东西吗?

最佳答案

可以使用单个图标代替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 channel ,所以它可以是您想要的任何颜色。 ContentContainer使用前景颜色,该颜色在按下时会更改,您可以通过更改按钮前景来更改图标颜色。此样式基本上是默认样式,但代替ContentControl,而是使用带有OpacityMask的网格。

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

关于silverlight - 如何正确使用WP7 sdk随附的图标?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3919882/

10-08 20:33