WPF Basics

Recently I have been working part time on a card game (simplified version of poker) using WPF. I had built some 2D games using Windows forms earlier but let me tell you, WPF is picture perfect for such applications. I have used Expression Blend for designing the game and Visual Studio 2008 for the code behind logic. (That’s a killer combination!). I would point out some important things which I feel WPF beginners would face while building a 2D game.

WPF Animation in code-behind

A 2D game made in WPF with no animation is hard to imagine to me. Expression Blend facilitates with hooking up events to a particular timeline(animation) created. But several times we might need to animate things in the code-behind. Here is a way to do it which I use a lot of times in my code.

C#

Storyboard story1 = (Storyboard)FindResource([key]);

Story1.Begin(this);



That was easy.



Mixing Windows forms with WPF forms.



Since I had few windows forms developed earlier which would be useful in this game, I thought of starting the application with a already existing windows form. This is not that difficult. A WPF application starts from the Main function located in the App.g.cs file (if C# environment) which is at the path projectrootdir/ obj/Debug. This Main function instantiates an object of class App,which is inherited from Application class, and runs it. The App.xaml file contains a StartupUri property to set the start form. Remove that property and a set an eventhandler for the Startup event of App class. In this event handler one can instantiate a form object as needed. The Application xaml tag looks somewhat like this:




xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Startup="Application_Startup">









I will be continuing this blog in later posts diggin into more complex parts. Be in touch.

Abhang Rane


No comments :

Post a Comment

Leave a Comment...