Programming Tutorials

Using the Tag attribute in XAML or “Where the hell can I bind this!?”

Warning: This blog post assumes some knowledge of XAML and Binding in WPF or Silverlight.

Whilst developing the Hull CS Blogs application for Windows Phone 7 I ran into the same issue several times. Some user interface items, such as Context Menu Menu Items in the Silverlight Toolkit don’t have attributes you can bind properties to in a way that makes sense. Heres an example bit of code to show what I mean

<!--A box containing a list item for the "Latest Blogs" page on the Main Panorama for the Hull CS Blogs app-->
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" Tap="blogTapped">
     <StackPanel Width="411">
          <TextBlock Text="{Binding Title}" Margin="12,-6,12,0" TextWrapping="Wrap" FontSize="30" Foreground="White"/>
          <TextBlock Text="{Binding Author}" Margin="12,-6,12,0" TextWrapping="Wrap" FontSize="20" Foreground="{StaticResource PhoneAccentBrush}" />
          <TextBlock Text="{Binding Abstract, Converter={StaticResource HTMLConvertor}}"  Margin="12,-6,12,0" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="#AFA7A0"/>

In this code we can see that affixing a Tap() event to the Title Textblock, which could then work out which Item was tapped by looking at the binding and comparing it to the Titles of blogs in a linked list in memory would be rather simple. But what if we want the user to be able to touch any of the items in order to activate the Tap() event?

To do this we’re going to have to give the StackPanel a Tap() event. So now, anything inside it that is tapped will trigger that event. Thats great! But how do we work out which item was tapped? Theres nothing we can bind to on a StackPanel that can accept a text input in the same way that the Text attribute can on a Textblock.

Binding the title of the blog post to Name will cause an error whilst Initializing the page, due to how XAML works and binding the title to attributes such as Visbility, Width or AllowDrop both doesn’t work and doesn’t make sense.

Tag attribute to the rescue! According to MSDN “Tag is intended to provide a pre-existing property location where you can store some basic custom information about any FrameworkElement without requiring you to subclass an element.” In essence, Tag is a place for you to store information that doesn’t make sense elsewhere, but you need to be associated with that Element in order for it to function correctly. In our case we can bind our title to it, like so

<!-- Now we can tap our stackpanel 🙂 -->
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" Tap="blogTapped" Tag="{Binding Title}">

All FrameWork Elements have a Tag attribute, so that means most, if not all items in XAML do. So, when you’re thinking “Where the hell do I bind this?!”, just think about tag!


Mobile Application Development

Evil Squash for Windows Phone 7 Usage Statistics at (just over) 100 Downloads

I’ve always quite enjoyed reading Rick Brewsters usage statistics blog posts for Paint.NET so I thought it might be interesting to write a similar post about Evil Squash on the Windows Phone 7 Marketplace. I reached a milestone earlier this month of 100 downloads (not bad for an application that was originally intended to only be part of my coursework.) So here it goes:

Downloads by Market (Country)

Unsurprisingly most of my Downloads are from the United Kingdom with 56 out of a total of 112 coming from the UK.

Just behind this is the United States with 51 Downloads.

The third and last market with any downloads is rather surprising, Hong Kong with just 5.

Interestingly enough, though the app is available in Canada and Australia not a single download has taken place in either country.

Downloads by Day

As you can see from the feature image of this post most people downloaded the app immediately after its availability, probably due to being high in the “new games” section of the marketplace. Since then downloads have slowly trailed off, but this is to be expected.

The Future

Having started the application off as a Silverlight based app I have decided to move over to XNA which will more easily allow me to more easily implement some more of the features I want to be in the game (including the AI doing their own moves, Animations etc). The only issue is, I’ve never used XNA… yet. So I’m steadily learning from Rob Miles XNA 4.0 book.

The next version of Evil Squash will also have social network integration so you can boast of your wins.

Thats all for now,


Evil Squash Demonstration

Today at 5:00 it was my turn to do my Evil Squash Presentation and get graded on my ACW 2 for programming which counts towards my module grade. This consisted of showing Rob Miles making sure my game worked according to a test sequence we had been given and conformed to the specification, then looking at my enhancements and finally looking at my evidence of testing and user documentation (effectively a manual).

Fortunately, my game worked correctly on the test sequence and had all aspects that we had to have from the specification, this was necessary for the first 40% of the grade. We could gain a maximum of an additional 20% for enhancements, but I had added more than enough enhancements to get that including;

  1. AI Players (worth 15%)
  2. Sound Effects (worth 5%)
  3. Windows Phone 7 Silverlight Graphical User Interface (GUI) (most people did console based games) (worth 15%)
  4. “Photocounters” (allowing the user to take a picture of themselves to be used as their counter on the board) (worth 5%)

The user documentation and test report were worth 10% of the total grade each and rob seemed happy with both, except that my test report wasn’t quite detailed enough, this is where I lost 4% of the total grade.

So overall I got 96% for ACW 2 for programming! Cant ask for much more than that 😀 Good times.



University of Hull Windows Phone Camp

(Above you can see my Windows Phone 7 Version of Programming 1 ACW2 game Evil Squash – The first screen shows the set up page where players select the number of human and computer players – The second screen shows the page where users enter their name and select the name of the AI player they want to play against. The third page is the actual Game UI)

Today was good fun, it all started with a somewhat boring IT and Professionalism lecture however which was about Computer Misuse & Software Liability and Contracts. After this we had a two hour break in which I worked on my Windows Phone 7 version of Evil Squash, which you can see above. After this break, which included a few well earn’t pints of coke, we had a computer systems lecture about multi-theading in the context of SPARC architectures, which Dr. Mike seemed rather animated about…

Welcome to Windows Phone Camp
Welcome to Windows Phone Camp Slide (with motion blur, which I totally meant to add and isn't due to my lack of photography skills)

After Computer Systems Rob, James, Lewis, Louis and I headed over to Robert Blackburn Lecture Theatre D in which Ben Nunney and Joanna Tong were ready to present our Windows Phone Camp. The 2 hour or so lecture consisted of information on:

  • How to develop Silverlight apps
  • Information on the ideas behind Live Tiles and Hubs
  • How to get add polish to applications and get them Marketplace Ready
  • How to submit applications to the marketplace
  • Requirements of the Marketplace
  • Advantages and Disadvantages of Windows Phone
  • The Convergence of Windows, Windows Phone and Xbox
  • The Metro Design Language
  • Using Blend and Visual Studio Side by Side
  • Launchers and Choosers
  • Making money on the Marketplace
  • much much more I’m sure I’ve forgotten to write down
  • and most importantly “Witty comments” by Ben 😉

Once the lecture had ended we all walked on to RB-312 for Tea (or in my case coke) and some programming time whilst we waited for our pizza (paid for by Microsoft!) to turn up. In this time I added custom counter functionality to my Evil Squash Application. My users can now use a “CameraChooser” to take a picture of themselves or an object to use as their counter on the gameboard, pretty cool I think!

Tonight I’ve been adding to the game.

Thats all for now,

PS: HTC updated all their phones, including my model – The HTC 7 Mozart – with Internet Sharing. Neat.