Accessing the canvas in a Silverlight 1.1 User Control

Posted on January 30, 2008
Filed Under 1.1 Alpha, Application, Components, User Interface, XAML | Leave a Comment

When you create a Silverlight User Control from a piece of XAML, you expect to be able to modify the Canvas using the a standard property. After all, the control’s XAML is a Canvas.

Foo.xaml
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="100"
Height="100"
Background="CornflowerBlue"
>
</Canvas>

However, the Control interface doesn’t have an accessor for the Canvas. Looking at the control in the debugger, you find that the Canvas is buried down deep in the control in a non-public area.

Control Canvs

So, how do you access the Canvas?
Well, there’s a simple 3-step process.


public class Foo : Control
{
// STEP1: Save the canvas locally
private Canvas _TheCanvas;


// STEP2: Add a public property so that clients can access
// the canvas easily.
//
// Control.Canvas
public Canvas Canvas
{
get { return _TheCanvas; }
}

    public Foo()
{
// STEP3: Save the canvas as the control is being created.
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("UserControls.Foo.xaml");
_TheCanvas = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd()) as Canvas;
}
}

That’s it. Now you can access the canvas of your control at any time.

Silverlight Viewports

Posted on November 14, 2007
Filed Under 1.1 Alpha, Components, Games, User Interface | Leave a Comment

One thing you usually recognize about web-based games is that they generally have a fixed view. With a fixed view, your game world matches the web control 1:1. What if you want the game to take place in a larger space, like an entire world, or a galaxy. In the game world, you’d create a viewport or a camera that only displays a portion of the world. Unfortunately, Silverlight doesn’t have native support for viewports into a larger space. We decided to make it.

CanvasViewport Class Diagram 

Source: CanvasViewport C# class 

The CanvasViewport is a simple plugin class that can be used to create a viewport on any Canvas (it doesn’t have to be the main page). It works by manipulating the Canvas’ RenderTransform in response to programmed parameters.

Usage:
// Create a viewport on this canvas.
CanvasViewport _Viewport = new CanvasViewport(this);

// Create a viewport that is 320x240 in logical (original canvas) units
_Viewport.Size = new Size(320, 240);

// Set the center of this viewport to 500, 800 on the canvas
_Viewport.Center = new Point(500, 800);

// In a game, you can have the viewport track a player like this:
void Update()
{
  _Player.Update();
  _Viewport.Center = _Player.Position;
}

An added advantage to using viewports in XAML is that since XAML graphics are all vector based, zooming in and out dosen’t have all of the pixelation problems that working with raster graphics has. Here’s the orignal game without the viewport.
 Shooter without viewport 

Here’s the game with a smaller viewport into the original world. (Notice that the UI and the Reticle doesn’t change size).
Zoomed in view of the shooter

Have fun with your viewports.

Silverlight Flyout Navigation

Posted on September 18, 2007
Filed Under 1.0 Javascript, User Interface | Leave a Comment


This example leans a little more towards traditional web site navigation with the tried and true flyout menu. I didn’t really have a need to make one of these but I thought it might come in handy in the future. I’ll also have to write an N-level version but I’ll put that off til later as the need for that rarley seems come up any more. 3 versions including source.

Flyout nav, hide/show - Example, Source

Flyout nav, fade in/out - Example, Source

Flyout nav, animate up/down - Example, Source

Silverlight Tile Navigation

Posted on September 17, 2007
Filed Under 1.0 Javascript, User Interface | Leave a Comment


Im gonna try to do the next couple of examples relating to navagtion elements. Ill start off with this one which uses image tiles for buttons. On rollover they animate and scale to give the illusion of depth. Reflections thrown in of course.

The tiles are generated and placed by code so the xaml file will be relatively empty.

Horizontal Tile Nav - Example, Source

Horizontal Tile Nav with stagered depths - Example, Source

Recently


Categories


Archives