简体   繁体   English

WP7-单击按钮后无法设置背景

[英]WP7 - Cannot set background of button once it has been clicked

I have this memory game app that I'm working on, trying to learn WP7. 我正在使用此记忆游戏应用程序,尝试学习WP7。 Imagine 6 uniquely colored buttons on the screen where the sequence has to be repeated by the user. 想象一下屏幕上有6个颜色独特的按钮,用户必须重复该序列。

  1. First pass, disable all buttons, change Button1's background 第一次通过,禁用所有按钮,更改Button1的背景
  2. All buttons enabled, user clicks Button 1, game continues... 启用所有按钮,用户单击按钮1,游戏继续...
  3. Second pass, disable all buttons, Button 1 and 3 are to be highlighted 第二次通过,禁用所有按钮,按钮1和3将突出显示
  4. Only Button3's background is changed (debugging shows that procedure to change Button1's background was run) 仅更改Button3的背景(调试显示已运行更改Button1的背景的过程)

The highlighting of the buttons is simply changing to a lighter background. 按钮的突出显示只是更改为较浅的背景。 Whatever button is clicked, it will not be available to change the background color in subequent passes. 无论单击什么按钮,在随后的传递中都无法更改背景颜色。

Style XAML: 样式XAML:

<phone:PhoneApplicationPage.Resources>
        <Style x:Key="ButtonStyle1" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <!--Define the states for the common states. The states in a 
                                        VisualStateGroup are mutually exclusive to each other.-->
                                <VisualStateGroup x:Name="CommonStates">
                                    <!--Define the VisualStates in this VistualStateGroup.-->
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver" />
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
                                        </Storyboard>                                        
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" To="1"/>
                                        </Storyboard>
                                    </VisualState>
                                    </VisualStateGroup>
                                <!--Define the states for the focus states. The states in a 
                                        VisualStateGroup are mutually exclusive to each other.-->
                                <VisualStateGroup x:Name="FocusStates">
                                    <!--Define the VisualStates in this VistualStateGroup.-->
                                    <VisualState x:Name="Focused" />
                                    <VisualState x:Name="Unfocused" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="{TemplateBinding Background}">
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Grid>
                        <!--The parts of the button control will be defined here.-->
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>

Button: 按键:

<Button Height="121" HorizontalAlignment="Left" Margin="14,61,0,0" Name="but1" VerticalAlignment="Top" Width="197" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" Click="but1_Click" />

Should I replicate a StackPanel/Rectangle to replace my button? 我应该复制一个StackPanel / Rectangle来替换我的按钮吗? Or is there a style or basic thing that I'm missing? 还是我缺少一种风格或基本的东西?

Solved. 解决了。

I changed the Pressed state of my button's style - this after having compared my style and the default style of a button. 在比较了我的样式和按钮的默认样式之后,我更改了按钮样式的“已按下”状态。 It would seem I was missing some code. 似乎我缺少一些代码。

<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" 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>

This was a stab in the dark :) 这是黑暗中的刺伤:)

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

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