I wanted to do this animation at runtime. I’ve noticed that XAML is great when you know exactly which pre-created object you want to animate in a pre-determined way. If you however haven’t created the object yet or want to use some custom parameters, using C# code can sometimes be a better way to achieve your goals.
Archive for April 26th, 2010
This is part 2 of the series “Silverlight in Action”:
- Visuals staring at the mouse cursor.
- Floating Visual Elements.
Here we’re sharing our experience from the amazing CompletIT web site.
As you may already know, every Silverlight application “must” have some objects floating around in a quite 3D manner. For example, let’s make a panel with happy faces floating around the screen in a controlled way, like this one.
See how it looks
Floating in Action
The idea is to create an attached behavior for a panel, so that when enabled, through attached property it’ll make all panel’s children float around. To make this effect we will use PlaneProjection and TranslateTransform. We’ll create Storyboard that will animate these properties, to random values (but in controlled manner with a threshold) and when it completes – the animation will begin again with changed “To” by random. This way we’ll create random continuous floating of the visual objects.
We need a class that holds the attached property. Let’s name it FloatingService. It has one attached property, for instance IsChildrenFloating of type boolean to trigger the behavior and two more properties to handle the Maximum allowed values for the PlaneProjection and TranslateTransform. So when IsChildrenFloating is attached to the panel with value of true, all its children will start floating.
This is how we control the animation in the FloatingService class.
So we create Storyboard from code, to hold the 5 animations – for rotating X,Y,Z and translating X,Y. And when the Storyboard finishes we choose new random values for end of the animations and start it again.
And now we only need to use the attached behavior to begin the floating of visuals.
How to use the behavior
Today , I will cover a very simple topic, which can be useful in cases we want to mock different interfaces on our expected mock object. Our target interface is simple and it looks like:
- public interface IFoo : IDisposable
- void Do();
Our target interface has an IDisposable implemented. Now, generally if we implement it in a class , we have to implement the whole of it [which is quite logical] and that should not wait for any extra calls or constructs. Considering that, when we are creating our target mock it should also implement all the dependent interfaces for us as well.
Therefore, as we are creating our mock using Mock.Create<IFoo> like that follows, it also implements the dependencies for us :
- var foo = Mock.Create<IFoo>();
Then, as we are interested only in IDisposable calls, we just need to do the following:
- var iDisposable = foo as IDisposable;
Finally, we proceed with our existing mock code. Considering the current context, I will check if the dispose method has invoked our mock code successfully.
- bool called = false;
- Mock.Arrange(() => iDisposable.Dispose()).DoInstead(() => called = true);
Further, we assert our expectation as follows:
- Mock.Assert(() => iDisposable.Dispose(), Occurs.Once());
Hopefully that will help a bit and stay tuned.