简体   繁体   中英

Custom Animation Port to Universal App from WP7 fails with SetTargetProperty

Porting custom animation code from WP7 to store app. WP7 code successfully performed a page flip animation of a border object with a bunch of text boxes on it (that was a page to be flipped.) In the below code Storyboard.SetTargetProperty does not compile complaining that it wants a string:

DoubleAnimation anima = new DoubleAnimation
{
    To = pageHostBaseIndex + 1,
    Duration = CalculateFractionalDuration(pageHostBaseIndex + 1)        
};

Storyboard storyboard = new Storyboard();
Storyboard.SetTarget(anima, this.PageTransition);
Storyboard.SetTargetProperty(anima, new PropertyPath(PageTransition.FractionalBaseIndexProperty));

storyboard.Children.Add(anima);
storyboard.Completed += Storyboard_Completed;

storyboard.Begin();

PageTransition derives from DependencyObject, it contains a DependencyProperty called FractionalBaseIndexProperty.

I tried putting in the string "PageTransition.FractionalBaseIndexProperty" as well as constructing a PropertyPath string. I also tried "(PageTransition).(FractionalBaseIndexProperty)" these all compile but fail with the exception:

No installed components were detected. Cannot resolve TargetProperty PageTransition.FractionalBaseIndexProperty on specified object. at Windows.UI.Xaml.Media.Animation.Storyboard.Begin()

I also tried EnableDependentAnimation = true, and making PageTransition derive from Timeline instead of DependencyObeject but these had no effect (same error.) The eventual animation is a little complex but I don't think it's getting that far. Seems like a Silverlight to Universal difference in objects acceptable for binding to a storyboard or in something with the path. I'll bet there's a more XAML friendly way to do this now but at this point I'm trying to minimize the port and I'd like to keep the feel of the currently animation.

Thoughts?

After hours of tinkering and searching and then reverting some of my previous tinkerings I finally got past this. This thread got me thinking:

Windows 8 - Animating custom property in code-behind .

My border objects were already declared in XAML but my storyboard, animation and PageTransition objs weren't. So I added the following XAML in my UserControl.Resources:

<Storyboard x:Name="storyboard">
    <DoubleAnimation x:Name="anima">
    </DoubleAnimation>            
</Storyboard>
<local:FlipTransition x:Key="Foo"></local:FlipTransition>

I used FlipTransition because in my case the PageTransition is an abstract class (so XAML wouldn't let me instantiate it.) FlipTranstion derives from PageTransition.

Along with that I had to re-create new storyboards and animation objects (see below) with the same names as the ones in the above XAML (I don't know why the ones I instantiated in XAML wouldn't work.)

I also had to set EnableDependentAnimation = true

And last, the Path in SetTargetProperty had to change to PageTransition. FractionalBaseIndex (instead of the original PageTransition.FractionalBaseIndexProperty.)

DoubleAnimation anima = new DoubleAnimation
{
    To = pageHostBaseIndex + 1,
    Duration = CalculateFractionalDuration(pageHostBaseIndex + 1),
    EnableDependentAnimation = true
};
Storyboard storyboard = new Storyboard();

Storyboard.SetTarget(anima, this.PageTransition);
Storyboard.SetTargetProperty(anima, "PageTransition.FractionalBaseIndex");

When all these came together it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM