Tuesday, June 22, 2010

Wpf Animations and Mvvm

One of the many topics I have been wanting to look into in more detail lately has been how animations specifically work with MVVM.  It seems to be more complicated than it first appears on the surface.  If the state of a controller (ViewModel) changes then the view follows suit by binding to a state property or at least using a DataTrigger etc.  The View either changes immediately to the new visual state (like show/hiding a panel) or beginning an animation to show or hide something. But what if you wanted to play the animation storyboard and once it finished then allow the state to change?

One of the best MVVM resources I have read recently is Josh Smith's Advanced MVVM mini-book.

In this book he talks about this very topic among many other relevant topics under the heading Advanced MVVM.

You can download his source code for this book here. Its a rather simple but cool game.


Chapter 5 of Josh's book talks about this very topic.  The challenge is to be able to trigger a series of animations each one starting only when the other has completed.  Each transition could take an unknown period of time. I won't steal his book's thunder, just a reference so the an example of a good answer can be found.

If I was to approach this, my simplistic approach simply would be to expose events on the controller for each phase passing through any necessary information as an EventArgs subclass. This isn't as elegant, but would still be suitable.  The challenge is effectively communicating back to the controller the view has completed its animation transition.  This could be achieved one of two ways:
1) Use the completed event on the storyboard to call a method on the controller.
2) Pass a delegate as a event args parameter which is called when on the storyboard complete event is raised.

The real question here is: Is there a better way using Blend4 with visual states? Like most things in life the question to one answer simply leads to at least 2 more questions.  I suspect not, as a visual state cannot help you define dynamic storyboards that change.

Monday, June 7, 2010

I have been doing a lot of talking about WCF lately, and thought it would be useful to have a good working example to show anyone interested.

This example makes use of NamedPipeBinding, of course this can easily be changed to any other kind of binding. The exposed services use both request/response and subscribe/publish using callbacks.  The request/response service uses asynchronous calls.  Unfortunately the NamedPipeBinding does not allow use of the Metadata-Exchange (mexHttpBinding) end-point to allow easy client-side discovery and client proxy code generation. In my example netNamedPipeBinding is the best to use seeing as the service is on the same machine.

For more information around choosing a binding see this great blog post  Here is a chart outlining selection of a binding from Soledad Pano's blog. And here's a handy reference to all different bindings and their matching config element (I always get the casing wrong first time) http://msdn.microsoft.com/en-us/library/ms731092.aspx.

Most developers I know respond best to a good extensive code example better than long drawn out talks with plenty of acronyms thrown in.  This example is a basic to intermediate use of WCF.


Use Instructions:
  1. If you're using Windows 7 be sure to Unblock the zip before extracting the files (yes you can trust me unless... you're on my black list LOL)
  2. Build the code. Then run the ServiceHostConsole project. This will host the services inside a Console application, then we can run the client application. The client application won't run if the services are not hosted and running somewhere.

  3. Once the console window appears and confirms all the end-points are up, you can detach the Visual Studio debugger.
  4. Now we can run the client application - press F5.

    The first panel "Dashboard" retrieves its data from net-pipe://myservice/status using the IStatusService interface.

    The second panel "Form" retrieves its data from net-pipe://myservice/contact using the IContactDataService interface.

    Here's an overall application schematic:

    Follow the code through, I hope you find it informative.

Sunday, June 6, 2010

Windows XP Sp3 Sunset

Microsoft Windows XP Sunset Policy and Lifetime page.

Basically boils down to sunset of mainstream support for XP SP3 in 2011, includes all support and release of Sps. Beyond 2011 Extended support will be available up to 2014, but Extended Support will be available to paying support subscribers only.

Thursday, June 3, 2010

Delay signed assembly problem

If you are using delay signing feature in Visual Studio, to defer the signing of your assemblies. You quickly run into problems trying to reference them during development.  Delay signing is used when the private key is closely guarded and developers do not have access to it.  Only the public key is available to partially sign the assemblies.  The final signing of the assemblies with the matching private key is done usually during the build process.

When you reference a compiled binary that is only partially signed during development (you would not do this outside of a developer environment), you will get an exception as follows:

FileLoadException as unhandled.
Could not load file or assembly 'AssemblyAbc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b4571042c8aa084e' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)

To enable this reference in Development, you can skip the certificate verification process using the Sn.exe tool.
sn.exe -Vr *,b4571042c8aa084e

Run that command in the Visual Studio 2010 Command Line and thereafter the reference will work.
The Vr option signifies you wish to skip verification for this assembly. * signifies any user, and the hex value is the public key token of the partially signed assembly.

If you're running a 64-bit installation of Vista you'll need to use the sn.exe located at C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sn.exe