WPF ColorAnimation in Code

In WPF you have the liberty to either create your animation in either XAML (xml format) or you can simply do it in the code behind file using C#. Here, I will also use a code-behind approach to animate the background Color of a button (say, buttonName).

SolidColorBrush brush = new SolidColorBrush(Colors.Red);
buttonName.Background = brush;

ColorAnimation animation = new ColorAnimation(Colors.Red, Colors.Blue, 
			   new Duration(TimeSpan.FromSeconds(5)));

animation.AutoReverse = true;
animation.RepeatBehavior = RepeatBehavior.Forever;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);

NOTE:  you cannot initialize brush to Brushes.Red (for example) because that object is frozen and cannot be animated.

Refer it for more info 


Restart a WPF Application

There are times when we simply need to restart our app. However, in wpf, there’s no Restart method in the class Application (as it is in the Windows.Forms). And so you have to restart your app. To do this I will have to use the reference of the System.Windows.Forms dll.

private void Restart()
    // from System.Windows.Forms.dll

Another way of re-starting the wpf application is to do the following


If you have a ClickOnce WPF application, you do not want to do this. In order to understand why, you need to understand how a ClickOnce application is normally launched.

The shortcut you click on in the Start menu is not a normal shortcut to an executable. It is actually an appref-ms file, which defines the entry point and location of the application. When you launch using this file, all the parameters in ApplicationDeployment are correctly initialized, and ApplicationDeployment.IsNetworkDeployed is set to true.

However, if you use the code above, you launch using the application’s entry executable instead of the appref-ms. This will cause ApplicationDeployment.IsNetworkDeployed to be false, and the properties will not be initialized as if they were a ClickOnce deployment. More Info

Hence the first approach should always be preferred.