locked
Canvas child animation in c++ code

    Question

  • I want to animate the position of child element in canvas.  I have the following:

    DoubleAnimation^ da = ref new DoubleAnimation();
    TimeSpan ts = { 10000000 }; // 1 second.
    Windows::UI::Xaml::Duration dur(ts);
    da->Duration = dur;
    da->EnableDependentAnimation = true;
    Storyboard^ sb = ref new Storyboard();
    sb->Children->Append ( da );
    Storyboard::SetTargetProperty ( sb, L"Canvas.Top" );
    Storyboard::SetTarget ( sb, _rect );
    ...
    da->To = 10.0;
    sb->Begin();

    However, the call to Begin() will generate the following exception:

    "No installed components were detected."

    What is the proper way to do this in the c++ code?


    SAP

    Thursday, May 16, 2013 3:37 PM

Answers

  • The syntax for animating the Canvas.Top property requires parenthesis enclosure like this "(Canvas.Top)"

    The following works for me:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Canvas >
                <Rectangle x:Name="_rect" Width="20" Height="25" Fill="Red"/>
            </Canvas>
            <Button Click="Button_Click">Start</Button>
        </Grid>

    void HelpWithAnimation::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	DoubleAnimation^ da = ref new DoubleAnimation();
    	TimeSpan ts = { 10000000 }; // 1 second.
    	Windows::UI::Xaml::Duration dur(ts);
    	da->Duration = dur;
    	da->EnableDependentAnimation = true;
    	Storyboard^ sb = ref new Storyboard();
    	sb->Children->Append ( da );
    	Storyboard::SetTargetProperty ( da, L"(Canvas.Top)" );
    	Storyboard::SetTarget ( da, _rect );
    	da->To = 10.0;
    	sb->Begin();
    }

    • Proposed as answer by jrboddie Thursday, May 16, 2013 7:06 PM
    • Marked as answer by SmokeAlarmPizza Thursday, May 16, 2013 7:24 PM
    Thursday, May 16, 2013 6:49 PM

All replies

  • The first argument to the SetTargetProperty and SetTarget should be da not sb.  Also place these statements before Append.

    Storyboard::SetTargetProperty ( da, L"Canvas.Top" );
    Storyboard::SetTarget ( da, _rect );
    sb->Children->Append ( da );

    Thursday, May 16, 2013 4:02 PM
  • Thank you.  I have now made that correction.  However I still get the same exception.

    SAP

    Thursday, May 16, 2013 4:11 PM
  • The syntax for animating the Canvas.Top property requires parenthesis enclosure like this "(Canvas.Top)"

    The following works for me:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Canvas >
                <Rectangle x:Name="_rect" Width="20" Height="25" Fill="Red"/>
            </Canvas>
            <Button Click="Button_Click">Start</Button>
        </Grid>

    void HelpWithAnimation::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	DoubleAnimation^ da = ref new DoubleAnimation();
    	TimeSpan ts = { 10000000 }; // 1 second.
    	Windows::UI::Xaml::Duration dur(ts);
    	da->Duration = dur;
    	da->EnableDependentAnimation = true;
    	Storyboard^ sb = ref new Storyboard();
    	sb->Children->Append ( da );
    	Storyboard::SetTargetProperty ( da, L"(Canvas.Top)" );
    	Storyboard::SetTarget ( da, _rect );
    	da->To = 10.0;
    	sb->Begin();
    }

    • Proposed as answer by jrboddie Thursday, May 16, 2013 7:06 PM
    • Marked as answer by SmokeAlarmPizza Thursday, May 16, 2013 7:24 PM
    Thursday, May 16, 2013 6:49 PM
  • I would suggest to use TranslateTransform rather than Canvas and making sure it is initialize BEFORE attempting to use sb.Begin()

    For me I had the same error as you and the detail after the error was that it wasn't able to resolve TranslateTransform.  That's true because I was setting all my storyboard and trying to start it but the RenderTransform was never set to anything before.

    Tuesday, December 31, 2013 12:56 PM