Saturday, February 04, 2012    
Blog  

OpenLight Blog

How To Start An Animation From View Model (MVVM)

Aug 1

Written by:
8/1/2010 6:07 AM  RssIcon

image

Live Example: http://silverlight.adefwebserver.com/animationfromIcommand/

Download Code Sample: [Here]

Starting an animation from a View Model when you are using View Model Style (MVVM) is very easy when using Behaviors.

View Model

Here is the code for the View Model:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Input;
 
namespace AnimationSample
{
    public class MainPageModel : INotifyPropertyChanged
    {
        public MainPageModel()
        {
            StartAnimationCommand = new DelegateCommand(StartAnimation, CanStartAnimation);
            ResetAnimationCommand = new DelegateCommand(ResetAnimation, CanResetAnimation);
        }
 
        #region StartAnimationCommand
        public ICommand StartAnimationCommand { get; set; }
        public void StartAnimation(object param)
        {
            AnimationState = "ON";
        }
 
        private bool CanStartAnimation(object param)
        {
            return true;
        }
        #endregion
 
        #region ResetAnimationCommand
        public ICommand ResetAnimationCommand { get; set; }
        public void ResetAnimation(object param)
        {
            AnimationState = "OFF";
        }
 
        private bool CanResetAnimation(object param)
        {
            return true;
        }
        #endregion
 
 
        #region AnimationState
        private string _AnimationState = "OFF";
        public string AnimationState
        {
            get { return _AnimationState; }
            private set
            {
                if (AnimationState == value)
                {
                    return;
                }
                _AnimationState = value;
                this.NotifyPropertyChanged("AnimationState");
            }
        }
        #endregion
 
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
 
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}

 

Basically it is contains two ICommands. One sets the AnimationState property to ON and one sets it to OFF.

The View

image

Layout the View.

image

Create an animation called “RollBall”.

image

Grab an InvokeCommand Action behavior, and drop it on the Start Animation From ICommand Button in the Objects and Timeline window.

image

Set the EventName to Click, and then click Advanced options next to Command.

image

Databind it to StartAnimationICommand.
 

Now, all this does is call the ICommand that will set the AnimationState property to ON. We now have to trigger the Storyboard to run.

 

image

Grab a ControlStoryboard Action and drop it on the LayoutRoot in the Objects and Timeline window.

image

Click the New Button next to TriggerType.

image

Change to DataTrigger.

image

Click Data bind next to Binding

image

Bind to AnimationState.

image

Enter ON for Value.

image

Set the ControlStoryboardOption to Play and Storyboard to RollBall.

image

Also bind the Reset button and the AnimationState indicator.

The diagram above shows what is bound to what.

The animation will now run when the Start Animation From ICommmand Button is clicked. You have to click the Rest button to run it again using that Button.

But, I Want The AnimationState To Reset Automatically!

image

You can use an InvokeCommand Action behavior, that uses a DataTrigger to automatically call the ResetAnimationCommand (that will set the AnimationState back to OFF) whenever it is set to ON.

You can download that version [from here]

3 comment(s) so far...


Gravatar

Re: How To Start An Animation From View Model (MVVM)

Another very similar way is already implemented in the nRoute framework as a ReverseCommandTrigger, which is basically a trigger working off a ICommand's Executed event. That way you don't need an additional "state" property.

By Adrian Hara on   8/2/2010 6:40 AM
Gravatar

Re: How To Start An Animation From View Model (MVVM)

@Adrian Hara - Thank you for that information. Also, my simplistic example does not properly illustrate that this is not actually for the ICommand, but is really meant to handle a situation where you make a web service call and after it returns, it can change a property in the View Model and an animation will run.

By admin account on   8/2/2010 6:42 AM
Gravatar

Re: How To Start An Animation From View Model (MVVM)

What this really does, is show a designer (and developer) work together harmoniously. The developer can write his code in such a way, so that the designer is not restricted by it when hooking it up in blend. This article is also another welcome addition to Michael's “expanding” step by step designers tutorials! Answers to real issues, not just flashy demo’s with no real application! Thanks & keep it up!!! :-) But please think about contrast with your screens shots Michael (Does it say “Animation Sample”? I’m not sure…) - I think you did it to irritate me! LOL

By Evil Beezle on   8/7/2010 9:03 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use