我动态创建了故事板。我想找到故事板名称和统计数据并停止故事板动画?

时间:2014-10-21 12:07:25

标签: c# wpf mvvm

请看我的代码,我做了什么错误。这是列表玩家加入游戏。我想根据列表分配玩家。我想设置动画以使图像边框闪烁。

private void SetVisibilityForSeat(List playerseat) {     尝试     {

    for (int i = 0; i < playerseat.Count; i++)
    {
        ColumnDefinition seatGridcol = new ColumnDefinition();                
        ParentGrid.ColumnDefinitions.Add(seatGridcol);
    }


    string cardimage = AppDomain.CurrentDomain.BaseDirectory + "Images\\Cards\\CardTheme 1\\Blue_Back.png";           
    int incrementer = -1;
    int j = 1;
    foreach (var player in playerseat) // Loop through List with foreach
    {


        incrementer = incrementer + 1;

        seatGrid = new Grid();
        RowDefinition gridRow1 = new RowDefinition();
        seatGrid.RowDefinitions.Add(gridRow1);
        RowDefinition gridRow2 = new RowDefinition();
        seatGrid.RowDefinitions.Add(gridRow2);
        RowDefinition gridRow3 = new RowDefinition();
        seatGrid.RowDefinitions.Add(gridRow3);

        ColumnDefinition coldef1 = new ColumnDefinition();
        seatGrid.ColumnDefinitions.Add(coldef1);



        seatGrid.HorizontalAlignment = HorizontalAlignment.Center;
        seatGrid.VerticalAlignment = VerticalAlignment.Bottom;
        Grid.SetRow(seatGrid, 0);
        Grid.SetColumn(seatGrid, incrementer);
        ParentGrid.Children.Add(seatGrid);



        TextBlock tbName = new TextBlock();
        tbName.Text = player.NickName;                
        tbName.FontFamily = new FontFamily("Arial");
        tbName.FontSize = 18.667;
        tbName.TextAlignment = TextAlignment.Center;
        tbName.TextWrapping = TextWrapping.Wrap;
        tbName.Foreground = Brushes.Black;
        Grid.SetRow(tbName, 0);
        Grid.SetColumn(tbName, 0);
        seatGrid.Children.Add(tbName);


        SolidColorBrush redBrush = new SolidColorBrush();
        redBrush.Color = Colors.Black;

        border = new Border();
        this.RegisterName(("myAnimatedBorder" + j), border);
        border.BorderThickness = new Thickness(5);
        border.BorderBrush = Brushes.Red;
        border.Width = 110;
        border.Height = 150;
        Grid.SetRow(border, 1);
        Grid.SetColumn(border, 0);



        SolidColorBrush blueBrush = new SolidColorBrush();
        blueBrush.Color = Colors.Blue;
        SolidColorBrush blackBrush = new SolidColorBrush();
        blackBrush.Color = Colors.Red;
        Rectangle rect = new Rectangle();
        rect.Width = 104;
        rect.Height = 143;
        Grid.SetRow(rect, 1);
        Grid.SetColumn(rect, 0);
        rect.StrokeThickness = 4;
        border.Child = rect;

        Uri imgsource = new Uri(cardimage);
        BitmapImage bim = new BitmapImage();
        bim.BeginInit();
        bim.CacheOption = BitmapCacheOption.OnLoad;
        bim.UriSource = imgsource;
        bim.EndInit();
        Image image = new Image();
        image.Source = bim;
        image.Width = 100;
        image.Height = 139;
        Grid.SetRow(image, 1);
        Grid.SetColumn(image, 0);
        border.Child = image;

        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 1;
        da.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        da.AutoReverse = true;
        da.RepeatBehavior = RepeatBehavior.Forever;


        sb = new Storyboard();
        sb.Name = ("mystory" + j);
        sb.Children.Add(da);
        Storyboard.SetTargetName(da, ("myAnimatedBorder" + j));
        Storyboard.SetTargetProperty(da, new PropertyPath(Border.OpacityProperty));

        myBeginStoryboard = new BeginStoryboard();
        myBeginStoryboard.Storyboard = sb;

        seatGrid.Children.Add(border);               
        sb.Completed += new EventHandler(StoryboardCompleted);

        sb.Stop(this);            

        //EventTrigger myBorderLoadedTrigger = new EventTrigger();
        //myBorderLoadedTrigger.RoutedEvent = Border.LoadedEvent;
        //myBorderLoadedTrigger.Actions.Add(myBeginStoryboard);


        Grid subgrid = new Grid();
        RowDefinition subgridRow1 = new RowDefinition();
        subgrid.RowDefinitions.Add(subgridRow1);
        ColumnDefinition subcoldef1 = new ColumnDefinition();
        subgrid.ColumnDefinitions.Add(subcoldef1);
        ColumnDefinition subcoldef2 = new ColumnDefinition();
        subgrid.ColumnDefinitions.Add(subcoldef2);
        // subgrid.Width = 155;
        subgrid.Height = 56;
        Grid.SetRow(subgrid, 2);
        Grid.SetColumn(subgrid, 0);
        seatGrid.Children.Add(subgrid);


        arrow = new ArrowView();              
        arrow.Name = ("myarrow" + j);
        arrow.Width = 40;
        arrow.Height = 42;
        Grid.SetRow(arrow, 0);
        Grid.SetColumn(arrow, 0);
        subgrid.Children.Add(arrow);


        Rectangle countrect = new Rectangle();
        countrect.Width = 36;
        countrect.Height = 36;
        LinearGradientBrush yellowGreenLGBrush = new LinearGradientBrush();
        yellowGreenLGBrush.StartPoint = new Point(0.5, 0);
        yellowGreenLGBrush.EndPoint = new Point(0.5, 1);


        GradientStop orangeGS = new GradientStop();
        orangeGS.Color = Colors.Orange;
        orangeGS.Offset = 0;
        yellowGreenLGBrush.GradientStops.Add(orangeGS);

        GradientStop yellowGS = new GradientStop();
        yellowGS.Color = Colors.Yellow;
        yellowGS.Offset = 1;
        yellowGreenLGBrush.GradientStops.Add(yellowGS);

        countrect.Fill = yellowGreenLGBrush;
        Grid.SetRow(countrect, 0);
        Grid.SetColumn(countrect, 1);
        subgrid.Children.Add(countrect);

        TextBlock tbcount = new TextBlock();
        tbcount.Text = "50";
        tbcount.Width = 20.834;
        tbcount.Height = 19.249;
        tbcount.FontFamily = new FontFamily("Arial");
        tbcount.FontSize = 18.667;
        tbcount.TextAlignment = TextAlignment.Center;
        tbcount.TextWrapping = TextWrapping.Wrap;
        tbcount.Foreground = Brushes.White;
        Grid.SetRow(tbcount, 0);
        Grid.SetColumn(tbcount, 1);
        subgrid.Children.Add(tbcount);

        j++;

    }
   demo();

}
catch (Exception ex)
{

}

}

1 个答案:

答案 0 :(得分:0)

如果你只有一个动画,那么使用Storyboard几乎没有意义;故事板实际上是用于组合多个动画以及共享时钟/时间线。您可以使用元素的BeginAnimation方法直接在目标元素上应用动画。要在动画完成之前删除它,只需再次调用BeginAnimation并传递null作为动画参数。

有关手动控制动画的详细信息,请参阅this MSDN article