c# - WP7 - 自定义按钮中的闪烁背景

标签 c# windows-phone-7 animation storyboard custom-controls

这是我的自定义按钮:

<Style TargetType="local:AnswerButton">
    <Setter Property="Background" Value="{StaticResource BlueGradient}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:AnswerButton">
                <Grid>
                    <Border BorderBrush="Blue" BorderThickness="2" CornerRadius="10">
                        <Border Name="myBorder" Background="{TemplateBinding Background}" CornerRadius="9">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0"
                                       TextAlignment="Center" VerticalAlignment="Center" 
                                       Text="{TemplateBinding Option}" Foreground="Yellow" />
                                <TextBlock Grid.Column="1"
                                       TextAlignment="Left" VerticalAlignment="Center" 
                                       Text="{TemplateBinding Text}" Foreground="Black" />
                            </Grid>
                        </Border>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如何为自定义按钮设置动画?我试过这个:

    ColorAnimation myColorAnimation = new ColorAnimation();
    myColorAnimation.From = Colors.Blue;
    myColorAnimation.To = Colors.Green;
    myColorAnimation.AutoReverse = true;
    myColorAnimation.Duration = TimeSpan.FromSeconds(1);


    Storyboard.SetTargetName(myColorAnimation, "myBorder");
    Storyboard.SetTargetProperty(myColorAnimation,
        new PropertyPath(Border.BackgroundProperty));
    Storyboard myStoryboard = new Storyboard();
    myStoryboard.Children.Add(myColorAnimation);

    myStoryboard.Begin();

但是目标名称有问题。我知道这是错误的,但如何将目标设置为自定义控件?我的页面中有 4 个,我想为所选的一个设置此动画:

    <Controls:AnswerButton Name="btnAnswerA" Tap="AnswerButton_Tap"/>
    <Controls:AnswerButton Name="btnAnswerB" Tap="AnswerButton_Tap"/>
    <Controls:AnswerButton Name="btnAnswerC" Tap="AnswerButton_Tap"/>
    <Controls:AnswerButton Name="btnAnswerD" Tap="AnswerButton_Tap"/>

我不关心它是在代码中还是在 xaml 中,但是如何通过颜色动画让一些自定义按钮闪烁(闪烁)?谢谢

编辑: 我用 VisualStateManager 尝试了很多选项,就像 Thaven 的答案一样,但没有帮助。真的没有人知道哪里可能出现问题吗?

最佳答案

我有一个非常简单的想法,希望它对您有好处。每个按钮都有点击事件,对吗?

 private void Button1_Click_1(object sender, RoutedEventArgs e)
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += dispatcherTimer_Tick;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
        dispatcherTimer.Start();

    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {

        Random rnd = new Random();            
        Button1.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(
                      Convert.ToByte(rnd.Next(255)),
                      Convert.ToByte(rnd.Next(255)),
                      Convert.ToByte(rnd.Next(255)),
                      Convert.ToByte(rnd.Next(255))
                      ));             
    }

关于c# - WP7 - 自定义按钮中的闪烁背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029690/

相关文章:

c# - 使用 NPOI 在 Excel 电子表格中创建图表

xaml - 如何隐藏 PivotItem?

c# - 在 Windows Phone 上实现可滚动表

javascript - 添加内容时动画 div

c# - VSTS 无法构建 docker 镜像

c# - Bitmap.GetPixel() 返回错误值

windows - Windows 8 Metro 应用程序是否需要对代码进行任何更改才能在基于 ARM 的设备上运行?

ios - 如何向图像 iOS 添加连续正弦波掩码?

android - 使用 postDelayed 时未触发 AnimationListener 的 onAnimationEnd

c# - 捕获异常并在我的自定义错误处理程序中处理