MVVM, Silverlight, Xamling

Core Series Part 2b – Automatic template selection (this time real automatic selection!)

In the previous post I demonstrated the concept of automatic template selection in a very very simple way. The post was designed to demonstrate the concepts that will be applied in this post – you really shouldn’t use the methods described in the previous post in real projects!

This series is starting out covering off some very basic concepts that are important to understand before progressively up the complexity until we jump in the a full solution – so be patient if you find this stuff a little simple for your tastes!

In this post I will demonstrate a very simple way to apply convention to automatically select a View for the current ViewModel.

Convention is great – as long as you follow the convention (creating files with the correct names, and putting them in the correct spot) this it will “just work”. Yeah I know, never say it will “just work” – phrases like that curse software projects 🙂

Once again to reiterate, the ViewModels do not have any idea what View will be displayed… and you should always try to achieve this in your projects. As soon as the ViewModel knows about the View you are risking breaking all manner of software dev rules, but more importantly your code will lack elegance and be far harder to create and maintain. With this in mind, we “publish” the current ViewModel through a property in the “frame” – the frame being the VM (ViewModel) that looks after creating and displaying child VM’s, and eventually in later posts navigation and tombstoning etc. The VM is then picked up by a ContentControl which will then figure out which View to show. The frame is on the VM side of the fence, the ContentControl is on the View side of the fence. The link between the fence is DataBinding.

Example – automatic view selection using convention

First, you can download the example here.

You can view a video run through of the example here.

The code examines how to pass in a VM to the ContentControl and automatically resolve the view to show.

There are two concepts that are important to take away from this example: first is convention. Creating Views called xxxView and VMs called xxxViewModel. The ContentTemplateSelector is then called upon to look at the type names, figure out the View to show (by replacing ViewModel with View from the original VM type name), then dynamically building some XAML which contains a DataTemplate.

The second concept is of course the dynamic generation of a DataTemplate. It’s important to note that child controls in DataTemplates are not ingratiated when the DataTemplate is created – it’s not until the DataTemplate is applied against some content (in this case the VM) in the ContentControl. This is nice, it means we can cache the DataTemplate to negate the need to re-generate them when the same VM type is used next time.

The XamlReader is used to process the dynamic XAML and returns a DataTemplate, which the ContentControl sets to it’s ContentTemplate property at runtime.

So what’s the benefit?

Convention is great for writing code and building projects *fast*. To add a new View using this system takes minutes (if not seconds). Navigating to the new view is very simple also.

In the next post we will example some further benefits of letting the “View” side of the fence handle control resolution – mainly automatic, designer friendly, centrally managed content transitions!

MVVM, Silverlight, Xamling

Core Series Part 2 – Automatic template selection – marrying a view to a view model

This is the second part of my series on a different approach to MVVM. I highly recommend you read part one to get some background.

This series introduces concepts around MVVM – mainly around view and viewmodel selection, navigation and transitions. The concepts here can be applied to WP7, Silverlight or WPF (including Surface). Further on in the series I will demonstrate how simple more advanced topics like circular navigation and tombstoning can be made (very) simple. All of the techniques I talk about in this series are implemented in XamlingCore – an simple reference MVVM framework. You may choose to use XamlingCore or take the concepts from this series and use them as you please. I recommend you at least have the XamlingCore code available on your system as I will reference files here and there from the framework.

Keeping things simple

A big part of MVVM is figuring out how to match up the view to the view models that you create. Typically for each view, there is a view model (simple right??). Making this process as intuitive and straightforward as possible is very important.

XamlingCore works a little bit different than you may be used to. In XamlingCore you first create a viewmodel and then navigate to your new viewmodel… this is a little different than other systems – where you first create a view and the viewmodel is then “discovered”. This is called ViewModel first navigation.

I really like ViewModel first navigation – for a few simple reasons. Firstly – your code is completely agnostic to what the view is doing… in fact you could create all your ViewModels and navigate around your application without any views being generated at all! This makes it really easy to test. Secondly the very fact that the ViewModel side is completely view agnostic means that you are achieving a very high level of separation of concerns. It also allows for a really simple navigation system (you just keep your ViewModels in a stack, then pop them as the user navigates back).

The main reason I like this method however is that you create ViewModels just like you would any other object, calling constructors, properties etc to pass around data – and I’ll show some great things you can do with this later.

It may sound a little complex, but in practice it’s actually *really* simple.

ViewModel/View Marriage

The main concept we want to achieve is that ViewModels only worry about themselves, and the view side of the fence handles everything to do with choosing how to display the ViewModels (which templates, user controls etc to choose). This keeps the ViewModels very simple. The view stuff makes sense on the “view side”.

At the top level of our application there will be a NavigationFrame – much like the in built navigation system in Silverlight/WP7. The navigation frame has both View and ViewModel side components. It is responsible for taking the current ViewModel and displaying the appropriate view.

The basic workflow is: Construct new ViewModel -> Navigate to it -> Navigation presents the ViewModel to the frame -> frame resolves and creates view (via a DataTemplate) -> hands off ViewModel and new DataTemplate to a content control for display.

A really simple example

I have prepared a really simple example that demonstrates this basic workflow to give you a more hands on idea about how this would work.

Download the sample here.

View the quick screen cast here.

Obviously this sample wouldn’t work very well in the real world, but it demonstrates the concept of choosing a template based on the ViewModel type that is being shown in the ContentControl.

In the next part we will move on to a more automated, convention based approach!

Happy MVVMing!

MVVM, Silverlight

Core Series Part 1 – XamlingCore – a cross platform MVVM framework for Silverlight

Get the code from Codeplex

Well I’m the first to admit I’ve been rather slack with my blog lately! It’s for good reason I promise!

We’ve started a new business (Xamling) which is running nicely now, with three or four decent sized projects behind us now. Oh, if you think our site is a little plain, there is a new site coming soon!

It’s been mega busy the last couple of months, two of our big projects have been Windows Phone 7 launch apps for *big* companies – explaining my lack of blogging!

But it’s not all bad…

XamlingCore!

I was tasked a while ago with building a demo for Remix Australia using MS Surface, Phone 7, desktop and touch based slate PC’s. It was a very big job, and that was without the 20 (yes twenty) day time limit!

With this in mind, I had to re-think the way I build applications. The tech included Silverlight, WPF (with Surface SDK) and Silverlight for Windows Phone 7…

The framework has since been used in a range of production projects in Silverlight desktop and WP7…

The end result is a little framework called XamlingCore which allows you to build MVVM based applications on all these platforms very quickly and simply.

It’s doesn’t have all the bells and whistles of other MVVM frameworks, but XamlingCore has pragmatism at it’s heart – think of it as the pragmatic MVVM framework. In fact, we suggest using something like MVVM light for most of the base level MVVM stuff, like commanding and event aggregation etc – in conjunction with XamlingCore… why re-invent the wheel.

Whaaaa?

At it’s most basic level, XamlingCore is a set of classes coupled with convention to allow simple view selection and navigation. It’s a lot more than that, but that is it’s most important feature. This series of posts will demonstrate the various abilities of the framework.

One of the biggest problems facing a good MVVM framework is how to perform view selection. Another problem facing MVVM frameworks is that they must be simple enough to pass off to “end” developers. XamlingCore addresses these two issues. It’s extremely easy to use, and makes developing multi view applications extremely simple.

View Selection?

One of the tricky bits with MVVM is what is view selection / pairing them up with a ViewModel. Some call it the “triad” – the Model – View – ViewModel triad process. This process can be difficult to understand…

Normally in applications, the developer will decide they want to show View X, lets for example use a Login screen. In MVVM, the login screen will need a ViewModel, which will expose properties that the View (Login.xaml) will databind to. The process needs to somehow address:

  • Creation of a view
  • Creation of a view model
  • Pass in parameters to the view model
  • Join the view to the view model
  • Navigate to the view (once it is ready)
  • Some how transition to the new view (slide, 3d spinny thingo etc.)
  • Navigate back
  • Plus a whole lot more

So, there is a built in navigation framework in Silverlight and WP7 SDK’s right? Yes… but…

The navigation framework I’m referring to here only allows the developer to navigate to a URI, which points to a XAML file. It’s difficult/un-intuitive to then pair up the ViewModel… especially when you start navigating around a complex application (like navigating back etc). It also precludes you from a few “tricks” to make your dev effort even easier.

Separation of concerns

Any good developer will tell you separation of concerns is very important. In Silverlight, two such concerns are the View and the ViewModel. Each have very different semantics.

The idea in XamlingCore is to draw a very thick line between these two areas – let the view side worry about what to show, and let the ViewModel side worry about itself. It might not be immediately obvious what we mean by this, so read on 🙂

ViewModel first versus View first navigation

We briefly mentioned above the navigation framework in Silverlight – this is a view first scenario… you select the view to navigate to. Selecting a view kicks off the navigation process.

In XamlingCore, you select (create) a ViewModel then navigate to it… strange? Maybe at first – until you see how the view side is configured to handle this. The view side consists of a “NavigationFrame” which takes the selected ViewModel and automatically picks up which XAML file to show (creating it in the process).

This allows you to code in your ViewModel agnostically (i.e. pretty much ignore the view) – at the same time creating a really strong workflow with sample data etc for designers is very easy too. You can create Views and ViewModels very quickly and easily

This series of posts will go through how to get XamlingCore up and running, and show you how it can help you in your development effort. Most of the series will be screen cast based, with some text articles.

For now, check out XamlingCore on Codeplex – review the FlickrSample application to get started… first video coming very soon!

Silverlight, Xamling

And then there was light – Xamling is born

So the news is if you missed it: I quit my job at Readify and started a new company with my brother Alex.

Our new company is called Xamling (pronounced Zam-ling) ! Holy crap it took us ages to think of a name, we even ran a help name us site – which turned up some awesome results, but most of the good ones were already taken!!

Anyways, we finally settled on Xamling. Alex and I invented “Xamlings” – little flying Xaml icons for Remix 2009.

I like to think of Xamling as an anecdote: Fred – “Hey dude, what are you doing?” Jonsey – “I’m Xamling up some mad awesome”. Fred – “Oh sweet – want some pizza?”.

For those that don’t know Xaml stands for eXtensible Application Markup Language and is the markup that is used to build Silverlight and WPF applications (and Windows Workflows and other stuff too).

So, here is this new company – so what?

Well you may also know that Alex, who is not only my brother, is a very talented designer, who also happens to be a Xaml gun. So we decided to quit out jobs and try out something new.

What do we do?

Well – Alex and I have *a lot* of experience. At the moment Alex is working on UX design for Cochlea, and I’m working on some sekret stuff for Microsoft (not bad for our first gig in the new company!)

We specialise in Silverlight, MS Surface, WPF and Azure. But that is not what differentiates us…

What makes Xamling special is that we have developed an amazing designer developer workflow technique that we employ on our projects. We’ve even presented on it at the Melbourne Silverlight Code Camp, Silverlight Designer and Developer Network, Web DU and soon at Remix Australia 2010.

So what can we do?

Well, we can consult (UX, Design, Architecture etc), build (we have exclusive access to a development team with experienced developers, testers and project managers – which we manage and train, tailored training (SL!) and also pre-packaged/tailored architecture solutions to get your new project up to speed quickly (drop in architecture and training)…

Find us on twitter – I’m @jakkaj and Alex is @agkdesign.

Fun!

Community, Events, RIA Services, Silverlight, Windows Azure

DigiGirlz, Deep Zoom and Azure

A few weeks ago I was tasked with coming up with something for the attendees at the first ever DigiGirlz event in Australia to play with. Something to get them a little excited about technology. Catherine Eibner came to me with some ideas that she thought would make for a compelling exercise and we came up with a cool Azure based Deep Zoom app!

We decided to take the Eventr (Codeplex | Blog | Blog) open source project I worked on last year for ReMIX Australia 09 and make it more accessible and dynamic (and brand it a little for the event).

What came out the other side was a dynamic Azure based DeepZoom creation application with auto updating Silverlight front end!

What does it do?

Firstly, check out the running application here!.

The Silverlight application shows a DeepZoom composition of all the photos that were taken at the DigiGirlz event.

The photos were all added to a Flickr Group which the Eventr application automatically scans. When new photos are added, the photos are downloaded in to Azure and processed in to a new DeepZoom composition.

The Silverlight client is notified that new images are available – it then highlights the new images and automatically reloads the DeepZoom composition from the server.

The system is configurable to scan a Flickr Group, perform a Flickr full text search or perform a Flickr tag search (or a combination of these).

The system has been designed to be “multi-tenanted” which means it can host more than one group of images… I.e. it could do DigiGirlz and another collection – all the images would be separated depending on the URL entered.

How does it work?

The application is hosted in Windows Azure and utilises a lot of what Azure has to offer.

  • It uses a WebRole to host the Silverlight application and services (which read from the DataBase).
  • All data is stored in a SQL Azure database. The data stored includes information about which images have been downloaded from Flickr, their metadata (title, description etc.) and their processed state (have they been included in the DeepZoom composition yet?).
  • Linq to SQL is used to access the SQL Azure database. You can access SQL Azure in the exact same way that you would access a normal SQL database. In fact when in development mode (local) I used SQL Express, and during deployment I changed my config to point to a SQL Azure instance… too easy.
  • WCF RIA Services was used to communicate from Silverlight to the server (where Linq to SQL is used to then go up to SQL Azure). The Silverlight client is very easily then able to get the metadata (title, description) from the database, as well as send data back (like view counts etc). All in all, it was a very simple task to get data from SQL Azure into Silverlight.
  • Azure Blob Storage is used to store the generated DeepZoom files. Blob storage is great because you can access the files directly using a URL, as well as get programmatic access to them to CRUD(create read update and delete) them.
  • To fire commands based on events from the user, the system uses Azure Queue Storage. Queues allow you to add an item in one place, and read it in another (only one thing can read an item)… so it’s great to fire a command once, and pick it up once to execute it. An example command in this system is “Clear”. Admins can clear out all the images/collections and data and start again.
  • To build the Deep Zoom collection, the system utilises an Azure Worker Role. The worker role polls the database every few seconds to get out the Flickr search configuration (i.e. which group or text search to scan). It then performs this search against Flickr. New images are downloaded and added to the SQL Azure database for later processing.
  • When the Azure Worker Role downloads an image, it is stored in a Windows Azure Drive. Once stored, it is processed using DeepZoomTools (a part of Deep Zoom Composer.

    Windows Azure Drive is important as DeepZoomComposer doesn’t work with streams (which is how Azure Blobs work)… it needs a drive letter. Azure Drive is great as it provides drive letter access to a special type of blob storage – meaning it will work in Azure!

    Once the composition has been built it is uploaded to Azure Blob storage for access from Silverlight (you cannot access Azure Drive files from outside the Windows Azure hosting platform – they are a special type of storage).

  • Can I test it out??

    Unfortunately at this stage I cannot release the code for this stuff, but – you can have a go at using this project yourself…

    Head over to the test Flickr group here. Add a photo the the group (KEEP IT CLEAN PLEASE!!).

    Then head over to the test URL and keep watching… your new images will show up in no time (under 2 mins)!

    Resources

    Azure Team Blog: http://blogs.msdn.com/windowsazure/
    Azure Storage Intro: http://msdn.microsoft.com/en-us/azure/cc994380.aspx
    SQL Azure Overview: http://www.microsoft.com/windowsazure/sqlazure/
    Windows Azure Overview: http://www.microsoft.com/windowsazure/windowsazure/
    WCF RIA Services Overview: http://www.silverlight.net/getstarted/riaservices/
    Get Started with Silverlight: http://www.silverlight.net/getstarted/
    Eventr DigiGirlz Demo: http://jak.cloudapp.net/Default.aspx?guid=764fbcd0-c15f-45f3-bda5-de3ed9081ce8

    Eventr Codeplex project: http://eventr.clodeplex.com

Community, SDDN, Silverlight

SDDN Tonight in Sydney

From Chris Anderson

“Just a reminder that the Sydney SDDN meeting is tonight at 6pm (for 6:30). If you haven’t done so, please register on the Facebook event here: SDDN Sydney on Facebook so we can order enough pizza. I’ll give a bit of a rundown of the outcomes from MIX10, and Jose will talk about developing for Windows Phone 7 (or the Zune Phone as I prefer to call it).

Oh, and bring your external HD / USB keys / laptops / (trying to think of something suitably silly for this entry, but got nothing at this time of night), as thanks to Justin King who consumed great slabs of bandwidth to download all the MIX10 videos (16GB worth apparently, so come prepared) will have them all available for download off an external HD. If Justin doesn’t make it then we have a backup of Ewen Wallace (aka @CADbloke) who has already grabbed them from Justin and will also bring them on an external HD. There is a ton of great content there, and is is your best chance to get them if you haven’t got them already and want to reserve your bandwidth for other potentially less reputable purposes.

And hey, if none of that got your attention, there’s free pizza! See you then!

Chris Anderson
Resident Sydney SDDN Rambler”