Silverlight: how to decode a uri and pass the arguments to your method

The problem is that you need to pass your parameters to your Silverlight class and hopefully deal with them. First and foremost, how do you recognise that you’ve been called? Well, via the OnNavigatedTo event… this event is fired for every Silverlight page.

OK, so we captured the OnNavigatedTo event. What next? Well, next we try to decode the uri that was passed to the SilverLight app. Let’s pretend for a moment that we were called via “http:<YourSource>?title=myTitle&search=special”

We now have two session variables set; title=myTitle and search=special

So, how do we decode these? The easiest way is to use the SilverLight libraries to query the NavigationContext object;

if (this.NavigationContext.QueryString.ContainsKey("title"))                 this.Title = this.NavigationContext.QueryString["title"];

if (this.NavigationContext.QueryString.ContainsKey("search"))                 searchType [...]

Silverlight 3; Building a navigation tree from static and dynamic data

The problem

Build a navigation tree that allows users to navigate to specific static pages, from static entries in the menus, and to pages that are built dynamically based on selections from the menu.

The Solution in a picture

The solution in code

Build the static menu selections in xaml, being sure to name each of the major headings with “Tag” entries;

<Grid x:Name="LayoutRoot">

<controls:TreeView x:Name="treeview"
BorderThickness="0"
Margin="0"
[...]

Silverlight 3 – Binding POCO objects to XAML

Obvious, really, but I spent ages chasing down how to bind some kind of POCO to a XAML display;

<Grid x:Name="LayoutRoot">

<Grid.RowDefinitions>
<RowDefinition Height="300" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
[...]

Silverlight 3 – handling exceptions in page views

How do you handle the situation where your new Silverlight page needs to shout a warning or error, and then die? No idea, but this is how I do it…

Originally, I was quite scared of my Silverlight pages throwing an exception and then I realised that this was just another object and if we throw an exception, hopefully let the user know, and bail out, then that’s fine.

So the pattern I am now working with is this;

Constructor – set up the load operations
Page_Loaded events – bind the XAML objects to the retrieved data

On any error; display a meaningful message, in a semi-modal window
Navigate back to a safe place (e.g. /home)

What do I mean by “semi-modal”? Well, from the user’s perspective, it [...]

Silverlight 3 Navigation – navigating from a UserControl

I’ve been toying with Silverlight over the last couple of weeks and, I have to say, it’s much more complete than ASP.Net ever was (**). Lots of new paradigms to get my head around, of course, but overall it’s a very complete solution for an RIA.

Anyway, to the point of this posting; the new (in Silverlight 3.0) Navigation Framework is great; it allows you to present a common interface and have new pages display in a know part of the browser window, supports browser back/forward, and URL rewrites. See here for more information.

If you are inside a page that is controlled by the framework, then you are able to call on the NavigationService.Navigate method to divert control to a new XAML [...]

Iterating through a bunch of folders and files

So you want to start at a top level folder, and then process all the folders beneath… Maybe you really do want to look at every file (maybe count the total size of the folder), maybe you want to process all the XML files there. The most obvious route is to recursively search through each folder;

static void Main(string[] args)
{
string startFolder = @"C:\temp";

List<string> contents = new List<string>();
foreach (string dir in [...]

Validating XML Files against XSD Schemas (especially for files that don’t reference the schema)

Wow, XML is a pig, isn’t it? Don’t get me wrong; it does everything I need it to do in describing multi-faceted data, but it’s a pretty steep learning curve.

At first, I used XPath and a lot of coded validation. Then I finally invested time in learning XML Schemas (XSDs) and that helped a lot because I could validate the entire document and, only when I knew it was valid, start pulling data out of it. At around the same time, I stumbled upon LINQ to XML and this shortened the code substantially.  Now, all I had to do was validate the document against the XSD and, if it passed, get to decoding it via LINQ and we’re done and dusted [...]

FileTreeView – a SequioaView-like Application

I've long been a huge fan of the SequoiaView application released by Technische Universiteit Eindhoven, which displays disk utilization in a beautiful squarified cushion treemap format. This was released in 2002 and does a great job of showing exactly what's eating the space on your disk, but it has one major drawback; if you point it at a 2TB volume with a million files, but you only want to see what's taking the space in a small corner of the disk, it reads the entire volume before displaying what you originally asked it to. So, I decided to write a C# alternative to SequoiaView, partly to help us find the big files in specific folders really quickly, and partly just as [...]

Horrible error running against a 32bit .Net Library from a 64bit application

So, this is my first new dev project on my shiny new Windows 7 64bit machine (having come from a 32bit Vista box). As it happens, I want to use the Microsoft Data Visualization Components, which are only available as a set of 32bit DLLs compiled sometime in 2006.

OK, all good so far – loaded ‘em up, referenced them, threw a TreeMap control on my form, compiled fine and then ran it, and… Bang!

Got a message;

Could not load type ‘Microsoft.Research.CommunityTechnologies.Treemap.NodeColor’ from assembly ‘TreemapGenerator, Version=1.0.1.38, Culture=neutral, PublicKeyToken=3f6121a52ebf7c82′ because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.

Turns out the problem is that I’m referencing a 32bit component from a 64bit application, and [...]

Downloaded CHM help file shows “Navigation to the webpage was cancelled”

Huh – that was an odd one. I copied a bunch of controls over to my Windows 7 box and the DLLs work, but the help files are stuffed;

It seems this is common problem, fixed by right-clicking the CHM file and clicking on an “Unblock” button which I’ve never seen before!

I can’t claim credit for this – found it at Rob Chandler’s blog here. Weird sense of priorities, huh? OK to copy and run any old DLL that you found lying in the bin, but a helpfile? Woooohhhh… no… that’s much [...]