我如何在C#中添加动画故事板到我的网页资源,然后再打电话了吗?(How can I add a st

2019-10-29 03:09发布

中心页是我的目标网页。 在Hubpage.xaml,我有3×3的网格,其中包含一个矩形,是一种我称为一个“细胞”。 在HubPage.xaml.cs,特别是在中心页()构造,创建用于每个小区一个情节串连图板:

CreateStoryboardForCell("Column0Row0");
CreateStoryboardForCell("Column0Row1");
...
CreateStoryboardForCell("Column2Row2");

我想一个故事板添加到Page.Resources,通常我会做在XAML,但我从C#尝试它。 现在,这里是CreateStoryboardForCell实现:

    private void CreateStoryboardForCell(string cellName)
    {
        // Create two DoubleAnimations, one for scaleX and one for scaleY, and set their properties.
        Duration duration = new Duration(TimeSpan.FromSeconds(0.2));
        DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
        DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
        myDoubleAnimation1.Duration = duration;
        myDoubleAnimation2.Duration = duration;

        Storyboard sb = new Storyboard();
        sb.Duration = duration;

        sb.Children.Add(myDoubleAnimation1);
        sb.Children.Add(myDoubleAnimation2);

        // Set the targets of the animations
        Storyboard.SetTarget(myDoubleAnimation1, Column0Row0);
        Storyboard.SetTarget(myDoubleAnimation2, Column0Row0);

        // Set the attached properties of ScaleX and ScaleY
        // to be the target properties of the two respective DoubleAnimations
        Storyboard.SetTargetProperty(myDoubleAnimation1, "(UIElement.RenderTransform).(CompositeTransform.ScaleX)");
        Storyboard.SetTargetProperty(myDoubleAnimation2, "(UIElement.RenderTransform).(CompositeTransform.ScaleY)");
        myDoubleAnimation1.To = 15;
        myDoubleAnimation2.To = 22;

        // Make the Storyboard a resource.
        try
        {
            pageRoot.Resources.Add("story" + cellName, sb);
        }
        catch (Exception e)
        {
            Status.Text = "Error adding storyboard resource for cell" + cellName + ": " + e.Message;
        }
    }

pageRoot是Hubpage.xaml:第x页:NAME =“pageRoot”等添加资源时,我没有得到一个例外,但我看不到的资源,当我设置的断点,所以我认为这是成功添加,如我可以看到数增加时,没有异常被抛出。

继续前进,我对每列单元格,在那里我推断的行和列数,并尝试启动添加到页面资源较早的相应的故事情节进行点击处理程序。 下面是代码:

    private void Column1_Cell_Click(object sender, RoutedEventArgs e)
    {

        Rectangle rect = sender as Rectangle;
        int x = (int)rect.GetValue(Grid.RowProperty);
        int y = (int)rect.GetValue(Grid.ColumnProperty);
        string storyboardName = "storyColumn" + y + "Row" + x;
        Storyboard storyboard = (Storyboard)FindName(storyboardName);

        storyboard.Begin();
    }

但FindName调用总是返回一个空故事板。 我缺少的是在这里吗?

Answer 1:

我已经写了你。我希​​望福利企业该代码

这里是代码:

   <Grid>       
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="Column0Row0"/>
        <ColumnDefinition x:Name="Column0Row1"/>
        <ColumnDefinition x:Name="Column0Row2"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Rectangle Fill="Pink" x:Name="rectangle" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"/>
    <Rectangle Grid.Column="1" Grid.Row="1" Fill="Coral" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"/>
    <Rectangle Grid.Column="2"  Grid.Row="2" Fill="Orange" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown"/>
</Grid>

后面的代码:

private void CreateStoryboardForCell(Rectangle target)
    {
        ScaleTransform trans = new ScaleTransform();
        target.RenderTransform = trans;
        DoubleAnimation anim = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
        trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim);
        trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim);

    }

    private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        CreateStoryboardForCell((Rectangle)sender);            
    }     


Answer 2:

请试试这个code.I始终使用该代码查找storyboard.It作品。

这里是代码:

FrameworkElement element = new FrameworkElement();
Storyboard sb=new Storyboard ();
sb = element.FindResource("Please write here Storyboard Key") as Storyboard;
sb.Begin(target element name , true);


Answer 3:

如果BeginStoryboard不WinRT.You支持可以使用以下code.I创造codedehind.Maybe就可以解决这样的问题动画;我没有在这个问题上的一些研究。 我想这code.It作品

这里是代码:

private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Storyboard storyboard = new Storyboard();

        ScaleTransform scale = new ScaleTransform(1.0, 1.0);
        animatedRectangle.RenderTransformOrigin = new Point(0.5, 0.5);
        animatedRectangle.RenderTransform = scale;

        DoubleAnimation scaleAnimation1 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
        DoubleAnimation scaleAnimation2 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
        storyboard.Children.Add(scaleAnimation1);
        storyboard.Children.Add(scaleAnimation2);

        Storyboard.SetTargetProperty(scaleAnimation1, new PropertyPath("RenderTransform.ScaleX"));
        Storyboard.SetTargetProperty(scaleAnimation2, new PropertyPath("RenderTransform.ScaleY"));


        Storyboard.SetTarget(scaleAnimation1, animatedRectangle);
        Storyboard.SetTarget(scaleAnimation2, animatedRectangle);

        storyboard.Begin();         

    }        


文章来源: How can I add a storyboard animation to my page resources in C#, then call it again later?