Search code examples
c#uwpbing-mapsuwp-maps

How to animate FillColor of MapPolygon (BingMap MapControl) UWP


I want to use a ColorAnimation on the FillColor Property of a MapPolygon.

I created a HeatMap with the BingMap Control for UWP Heatmap Preview

I got a function, where i calculate a new FillColor for each MapPolygon. I want now to use a ColorAnimation instead of just changing the FillColor from the old value to the new value.

//Instead of 
statePolygon.FillColor = backGroundColor;

//I want to use something like the following
Storyboard storyboard = new Storyboard();
ColorAnimation animation = new ColorAnimation();
animation.From = statePolygon.FillColor;
animation.To = newBackGroundColor;
animation.Duration = new Duration(new TimeSpan(0, 0, 0, 5));
storyboard.Children.Add(animation);
Storyboard.SetTargetProperty(animation, "(MapPolygon.FillColor)");
Storyboard.SetTarget(myStoryboard, statePolygon);
storyboard.Begin();

But with the storyboard code i always get an System.Runtime.InteropServices.COMException which tells me the Animation target not specified.

I tried a lot of values for the second parameter of the Storyboard.SetTargetproperty... nothing worked.

What am I missing?


Solution

  • There are two issues.

    In the code snippet you posted you are using myStoryboard in SetTarget instead of the storyboard variable. Fixing that seems to resolve the COMException. But the polygon color still will not animate.

    The reason is that the FillColor property is not a DependencyProperty. To enable animation of this property, you will have to enable dependent animation on your animation before starting the Storyboard:

    animation.EnableDependentAnimation = true;
    

    The difference between dependent and independent animations is described in MSDN documentation. Note that the performance of this animation may be a bit worse than for independent animations.