Sunday, May 3, 2009

No Geometry Resources in Silverlight 2

I wanted to create a hexagon grid. There is a good article on how to draw a hexagon grid using C# code here.

I wanted to create my grid in XAML. I used Excel to do the calculations. I also used Excel to generate the polygon points statement. The simplest hexagon in XAML I could come up is coded like this:

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="480"
    Height="480"
>
    <Polygon Stroke="#000000" StrokeThickness="1" Points="0,35,20,0,60,0,80,35,60,69,20,69"/>
</Canvas>

That looks like this:

My next step was to set up a nice radial fill. I put the fill as a static resource. The resulting code looks like this:

<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="480"
    Height="480"
>

    <Canvas.Resources>
        <RadialGradientBrush 
            x:Key="HexRadialFill"
            GradientOrigin="0.5,0.5" 
            Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
            <RadialGradientBrush.GradientStops>
                <GradientStop Color="LimeGreen" Offset="0" />
                <GradientStop Color="Green" Offset="1" />
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
    </Canvas.Resources>

    <Polygon Stroke="#000000" StrokeThickness="1" Fill="{StaticResource HexRadialFill}"
        Points="0,35,20,0,60,0,80,35,60,69,20,69"/>
</Canvas>

The resulting image looks like:

I now planned on making the polygon a resource. Unfortunately you can't do that in Silverlight 2. I looked into converting the polygon into a path and then putting that as a geometry resource. Alas, Silverlight 2 does not allow geometry resources.

I have a couple of options. I could make the hex a user or custom control. That would allow code reuse. But at this time, I just want a hex grid. The quick and dirty way is to just write some more macros in Excel and generate the entire grid as a bunch of polygon statements. I could do that probably quicker than it took me to write this blog. However, I am dedicated to quality. I would never allow a developer who worked for me to hack out something quick and dirty like that. That means I need to create a user control and code that up. It will take me longer to code up the user control. The advantage is the resulting code will be reusable (even though I doubt I will reuse it).

I now know what it was like to work for me. I am somewhat of a demanding manager. I want things done the right way with no quick and dirty solutions. Now I can say I apply that standard even to my own work.

1 comment:

Unknown said...

Peter Blois was able to do it in what looks like nothing but xaml in the minesweeper sample on the silverlight 2 beta 2 page that used to be up.

You can see the sample on his page on blois.us, although I didn't see the source posted there.