Charles Petzold free eBook is great, except that it doesn’t cover the use of Sliverlight-XNA tools that are in the current version of Windows Phone.
One of the most reliable source on how to use the Silverlight XNA hybrid is the MSDN Article at:
In he Silverlight/XNA hybrid, you are working with two tools: Silverlight which uses XAML for the Presentation layer, and the XNA layer which can do a presentation layer and high speed connection with GPU, etc.
In the default template you have a Main Page and a Game Page. The Main Page has a button that sends you to the game page which by default opens the default XNA “blue” game. The Game Page xaml and class (in my examples are CSharp, but could be VB).
When you look at the initial MainPage.XAML, the work is done in the following:
The Click event is the way that the XAML page communicates with the “code behind”, when there is an existing event you can right click on it and then select the “Navigate to Event Handler”, in this case you will see:
You are then transported to the following event, events vary, and this event was created by the template (or actually the author of the template):
This is how the template author decided to navigate to the Gamepage.xaml. There are a number of ways to do this, but this is efficient.
If you click on the button you then “Navigate” to the game page, and when that opens you get the XNA “blue” game or the cornflower blue screen that you would get with a regular XNA game. Like XNA, the class and class scope fields are constructed: Add the code or variables (a type of field):
Unlike XNA the first procedure fired is the OnNavigatedTo, here is the example of the OnNavigatedTo method. Here you would add things that need to be loaded, like images, etc.which is shown in the code below:
You will need to add two images to the Solution, right click on the content project and add your existing images, I added two images named enemy.jpg and friendly.jpg
Now add the OnUpdate code, this is identical to the MSDN Example, but we are going to make a change by “refactor” and extract method, see the steps after this entry:
Select the code you want to extract, as shown:
Then right click and select Refactor-Extract Method:
Enter a name for your refactor code:
You will get a method call in your OnUpdate methods and you will get a method with the name you gave it:
Now add code to the OnDraw method:
If you run your program, not much happens, just a black screen. See the next blog for the XAML code.