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

北城以北 提交于 2019-12-12 05:13:11

问题


HubPage is my landing page. On Hubpage.xaml, I have a grid of 3x3, containing a Rectangle, what I'm calling a "cell". In HubPage.xaml.cs, particularly in the HubPage() constructor, I create a storyboard for each cell:

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

I want to add a storyboard to the Page.Resources, normally I would do it in XAML, but I am attempting it from C#. Now, here is the CreateStoryboardForCell implementation:

    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 is from Hubpage.xaml: Page x:Name="pageRoot", etc. I do not get an exception when adding the resource, but I cannot see the resource when I set the breakpoint, so I assume it was added successfully, as I can see the count increasing, and no exception was thrown.

Moving on, I have a click handler for each column cell, where I infer the row and column number and try to start up the corresponding storyboard added to the page resource earlier. Here is the code:

    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();
    }

but the FindName call always returns a null storyboard. What am I missing here?


回答1:


I've written this code for you.I hope benefits to business

Here is code:

   <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>

Code Behind :

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);            
    }     



回答2:


Please try this code.I always use this code for finding a storyboard.It works.

Here is code:

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



回答3:


If BeginStoryboard doesn't support in WinRT.You can use the following code.I created the animation in codedehind.Maybe you can solve the problem like this;I did some research on this problem. I tried this code.It works

Here is code:

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();         

    }        


来源:https://stackoverflow.com/questions/26820227/how-can-i-add-a-storyboard-animation-to-my-page-resources-in-c-then-call-it-ag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!