WPF更改椭圆的填充颜色

时间:2016-10-20 06:06:49

标签: c# wpf

所以我试图制作8个圆圈,它们都有不同的填充颜色,并且都有不同的闪烁速度。到目前为止,我已经能够让它们以不同的速度闪烁,但是我在制作它们时遇到了麻烦,它们都有不同的颜色。到目前为止我的代码:

> dat$min_temp <- sapply( as.character(dat$Temperature), 
                    function(x) min( as.numeric( scan( text=x, sep=",", what=""))))
Read 10 items
Read 10 items
Read 10 items
Read 10 items
> dat
              City                                       Temperature
1 Edmonton,Alberta      4.1,13.6,15.2,15.7,14.2,15.2,16,14.2,17,13.1
2 Edmonton,Alberta     15,18.2,14.8,16.5,14.6,16.9,14.3,17.5,13,15.8
3 Edmonton,Alberta 15.8,17.9,16.9,15.1,13.2,13.1,16.8,12.4,14.7,15.6
4 Edmonton,Alberta     14.3,17.3,14.6,17.3,14.8,14,15.4,14.1,16,15.4
  min_temp
1      4.1
2     13.0
3     12.4
4     14.0

我的WPF:

    private void Appear(Ellipse element, double duration, Brush colour)
    {
        element.Fill = colour;
        DoubleAnimation db = new DoubleAnimation();
        db.From = 0.0;
        db.To = 1.0;
        db.Duration = new Duration(TimeSpan.FromSeconds(duration));
        db.RepeatBehavior = RepeatBehavior.Forever;
        element.BeginAnimation(Ellipse.OpacityProperty, db);
    }

    private Brush SetEllipseColour(Ellipse element)
    {
        Random rnd = new Random();
        int red = rnd.Next(0, 255);
        int green = rnd.Next(0, 255);
        int blue = rnd.Next(0, 255);

        Brush fillColour = new SolidColorBrush(Color.FromRgb((byte)red, (byte)green, (byte)blue));
        return fillColour;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        List<Ellipse> elements = new List<Ellipse>();
        elements.Add(Circle1);
        elements.Add(Circle2);
        elements.Add(Circle3);
        elements.Add(Circle4);
        elements.Add(Circle5);
        elements.Add(Circle6);
        elements.Add(Circle7);
        elements.Add(Circle8);

        Random rnd = new Random();

        foreach (Ellipse element in elements)
        {
            int r = rnd.Next(1, 10);
            double duration = (Double)r / 10;
            Appear(element, duration, SetEllipseColour(element));
        }
    }

注意:我知道我可以压缩/更改我的代码以使其更整洁或更好,但是现在我只想让颜色正常工作。

所以目前我的代码更改了所有 Ellipse元素的填充颜色,但我想将其更改为仅影响每个Circle。我该怎么做呢?

修改 对于那些我想要问的人感到困惑,我不知道如何单独改变每个圆圈的填充颜色。

2 个答案:

答案 0 :(得分:3)

在班级设置Random班级的实例,点击下面的示例,点击显示 Blink Em!Button动画被触发

XAML

<Window x:Class="Blink.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Blink"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Button x:Name="BlinkEm" Content="Blink Em!" Height="30" Click="BlinkEm_Click"/>
        <StackPanel x:Name="Container" Orientation="Horizontal" Loaded="Container_Loaded" Grid.Row="1"/>
    </Grid>
</Window>

代码隐藏

namespace Blink
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Random random = new Random();

        public MainWindow()
        {
            InitializeComponent();
        }

        public List<Ellipse> CreateCircles(int count)
        {
            List<Ellipse> circles = new List<Ellipse>();
            for (int i = 0; i < count; i++)
            {
                var circle = new Ellipse
                {
                    Height = 100,
                    Width = 100,
                    Margin = new Thickness(10),
                    Fill = Brushes.SkyBlue
                };

                circles.Add(circle);
            }

            return circles;
        }

        public void AddCircles()
        {
            var circles = this.CreateCircles(8);
            foreach (var circle in circles)
            {
                Container.Children.Add(circle);
            }
        }

        private void Container_Loaded(object sender, RoutedEventArgs e)
        {
            AddCircles();
        }

        private void BlinkEm_Click(object sender, RoutedEventArgs e)
        {
            foreach (Ellipse circle in Container.Children)
            {
                circle.Fill = GetRandomColor();
                circle.BeginAnimation(Ellipse.OpacityProperty, GetBlinkAnimation());
            }
        }

        public Brush GetRandomColor()
        {
            var red = Convert.ToByte(random.Next(0, 255));
            var green = Convert.ToByte(random.Next(0, 255));
            var blue = Convert.ToByte(random.Next(0, 255));

            return new SolidColorBrush(Color.FromRgb(red, green, blue));
        }

        public DoubleAnimation GetBlinkAnimation()
        {
            var duration = random.NextDouble();
            var animation = new DoubleAnimation
            {
                From = 0.0,
                To = 1.0,
                Duration = new Duration(TimeSpan.FromSeconds(duration)),
                RepeatBehavior = RepeatBehavior.Forever
            };

            return animation;
        }
    }
}

答案 1 :(得分:1)

我认为你的错误在于每次都会重新创建Random对象。尝试将其放入一个字段并初始化一次,或者将其作为参数发送到SetEllipseColor方法。

由于随机数生成器实际上并不是随机的,因此它们可以得到它们的初始值&#34;种子值的随机值,通常是当前时间。这意味着如果您在很短的时间内创建了一堆新的Random实例,它们很可能会以相同的种子结束,从而得到相同的值。

(更一般地说,&#34;出现&#34;和&#34; SetEllipseColor&#34;不是很好的方法名称。前者含糊不清,后者实际上并没有描述该方法正在做什么。)