ToggleButton Command for Prism

 Prism Commands enables you to handle user interactions in Xaml.  Even though the plumbing is there, Prism lacks commanding support for everything but Buttons.  Fortunately, it’s relatively easy to create new Commands for a range of controls.  This post details how to create a Checked and UnChecked Command for the ToggleButton (which can be applied to a RadioButton and a CheckBox).

Recently I had a need to create a Checked Command for a ToggleButton.  There are many sources on how to create a Command, the best one probably being Erik Mork’s video, but there wasn’t a place to grab the a Command from.  The intention of this post is to share the Checked and UnChecked Prism Commands for you to use it in your project.  (Moving forward, it might make sense to create a repository of these Commands.)

Finally, before diving in, if you just want the code and to see an example, follow these links:

Commands

Creating the Commands is straight forward.  Derive from CommandBehaviorBase and specify the type.  Then, in the constructor, register the event.  Below are both the Checked and UnChecked commands.

image

Extensions for Xaml

After creating the Commands you need to create Static methods to reference in Xaml.  The Static methods are named Checked and UnChecked.  They include a definition for:

  • SelectedCommandBehaviorProperty
  • CommandProperty
  • SetCommand
  • GetCommand
  • OnSetCommandCallback
  • GetOrCreateBehavior

In an effort to be brief I have chosen to truncate the two static methods.  To take a closer look at the code, click on the image or download the code.

Checked.cs
image

UnChecked.cs
 image

Hooking everything up

After the Commands are created, bind them to the ToggleButton in Xaml.  In this sample application there are three main files:

  • MainPage.xaml – the presentation. This contains the binding to the Commands.
  • MainPage.xaml.cs – the primary function of the code-behind is to new up the MainPageModel and set the DataContext.
  • MainPageModel.cs – this is the model for the MainPage. It defines the commands the Xaml binds to as well as a property for the TextBox to bind to.

Below are images of the classes mentioned above:

MainPage.xaml

Note in this Xaml, line 20 binds to the CheckedState property in the MainPageModel.  The commands are bound on line 22 and 23.  Take notice of the namespace command.  Instead of referencing Microsoft.Practices.Composite.Presentation.Commands it references ProjectName.CustomCommands.

image

MainPage.xaml.cs

This is the code-behind for the above UserControl.  On the Loaded, a new instance of MainPageModel is initialized and then set as the DataContext to the UserControl.  The heavy lifting is done in the MainPageModel.

image

MainPageModel.cs

As noted above, the MainPageModel does the heavy lifting.  The following describe what happens in this file:

  • Two DelegateCommands are defined for Checked and UnChecked. (line 36 and 41)
  • A Property called CheckedState is defined and fires the PropertyChanged event when updated. (lines 13 – 22)
  • Two methods are defined to handle the actions on the Commands. (lines 47 – 59)

image

The final product

Code | Example

image

Versioning your Silverlight application

When writing an enterprise level application it’s critical to have a version number.  Whether it’s located on the help screen or in debug mode, this will save you so much heartache during QA.  The larger the project, the more dependent your team is on the version.

Now that I’m all done ranting to the converted, let’s talk about Silverlight.  From my past experience there are two ways to handle versioning:

  1. Create an abstracted file that contains your version number, or
  2. Use the Assembly version.

Both have their pros and cons.  Option 1 gives you greater control of the version number, however the manual process is tedious.  Option 2 enables you to use the Assemblies version, however it provides less control over the version number.  Lately I’ve been going with option 2 because it takes one more step out of my deployment process.

To get the version from the Assembly

Doing this is fairly easy once you have the correct block of code.  The below block of code shows how to get the version from the Assembly in the MainPage_Loaded method.  Do not forget to include the System.Reflection reference.

image

Given the above block of code, the next question is, where is this assembly version defined?  In short, the AssemblyInfo.cs file, which is located in the Properties folder.

image

At the bottom of the AssemblyInfo.cs file, you’ll see the version info.  The directions are in the comments on how to change this.  I’ve customized my version to be 0.611.*.

image

Silverlight in Atlanta

A much needed thing happened tonight, I finally registered both AtlantaSilverlight.com and AtlSilverlight.com.  Over the past couple months, I’ve been running the Atlanta Silverlight Meetup. In July I’ll be organizing the Atlanta Silverlight Firestarter. 

Needless to say, there are a number of Atlanta Silverlight events that are budding, and as a result there needs to be a home to keep track of everything.  Events, information, and recaps will be placed on the newly registered domains (they’ll point to each other, but for some reason or another, I enjoy registering multiple domains).

Atlanta Web Design Group – May 27, 2009

Group Photo Album — Atlanta Web Design GroupSome house keeping because this post has been in my Live Writer for the past week…On May 27th I had the pleasure to present a MIX 2009 recap to the Atlanta Web Design Group along with Glen Gordon and Dennis Estanislao.  I was really impressed with the turnout, the location, and the people.  Everyone was into learning more what happend at MIX.

For more information, the slideshow and links presented, check out Glen’s post.   Now I leave you with a couple pictures from the nightphoto

Swag at the front and my friend Jeff Paries MIX session as a must see.

 photo (1)

Picture from the bathroom stall, lol.

 photo (2)

Dennis presenting

3 essentials to quickly learning Blend

The past few weeks I’ve been working with a great team at IQ Interactive.  Everyone is open and eager to learn Expression Blend and Silverlight, and I so badly want to teach/show them.  More often than not though, time is not a friend which makes it critical to identify what people need and want to see.

To paint the picture, I am introducing Silverlight to creatives, Flash devs, and people in the business.  Each group needs a different story.  At first I went about it the same way: naturally I would load Visual Studio, create a new project, knock out some Xaml, and then run the app.  This is not a good idea.  Instead, I found myself increasingly opening Blend and showing people around.  This lead to more interest and more questions.

Side note: Visual Studio is an amazing development tool, however I strongly believe Blend will become the tool of choice for developing Silverlight applications.  I don’t see this happening in for Blend 3 or 4, but Blend is making a compelling case to be THE tool for Silverlight dev.

All this background leads me to this conclusion: If you want to quickly pick up Silverlight, start with Blend (there is a 30-day free trial).  Additionally, here are the first 3 topics you should focus on once in Blend:

  1. Understanding the Layout Panels
  2. Animations
  3. States and Templating

(I realize this is a simplification.  Topics like databinding, integrating sample data, and wiring up events are important.  However, these three topics are a perfect starting point.)

Understanding the Layout Panels

There are three main Layout panels in Silverlight 3: the Grid, Canvas, and StackPanel.  The Silverlight Toolkit offers more layout types such as DockPanel, WrapPanel, and ViewBox.  Here’s a brief look at the three main panels:

Canvas – the most basic panel.  Elements are placed at explicit coordinates.

Grid – the most flexible of the three.  Elements can be arranged in rows and columns.  Additionally children can grow dynamically within the Grid.

StackPanel – arrangement of children elements are handled automatically.  The StackPanel is a great option when you have a list of items.

For a more in depth look at the Layout Panels check out Scott Guthries post.

Layout Panels in Blend

Once you know and get comfortable with how each panel works, you’ll be able to make the best decision for your application.  The tendency for Flash developers might be to go straight to the Canvas, then handle updating the layout from code.  I urge you to take some time to play around with the other panels

In Blend, you can draw panels on the Artboard by selecting a panel from the Toolbox.

1349-412662d845907f6e

Animations

Getting comfortable with animating elements in Blend takes a little getting use to.  The first time you try to add an animation it takes a minute to locate the add animation button.  It’s the plus sign at the top of the Objects and Timeline panel:

image

After you’ve created the Storyboard, the Artboard gets a red outline.  This lets you know you are recording.  Any property changed while recording will be added to the animation.  It took me some time getting use to this.

A tip to working with animations is to change Workspace to an Animation friendly view.  Do this by selecting Window > Workspaces > Animation.  You’ll notice, the Timeline stretches out at the bottom.

image

States & Templating

Finally, in my opinion, the most important thing to understand Sttes and Templating.  The reason I think it’s so important is because of Silverlight’s rich control set.  Understanding Templating and States enables you to use existing controls and completely change the visuals while still maintaining it’s base States and functionality.

This topic is very deep and much better served in it’s own post.  For the time being here are a couple links about States and Templating.

What are States - http://kokchiann.wordpress.com/2008/06/05/visual-state-manager-new-feature-of-expression-blend/

Templating a ToggleButton to look like a playhead for a video player - http://www.85turns.com/2008/09/05/making-a-play-pause-button-in-silverlight/

Speaking at CodeStock 2009

I’m proud to announce that I’ll be speaking at this years CodeStock.  The sessions were selected by a fresh and unconventional method.  All attendees, when registering, had the opportunity to vote for three sessions.  Out of 60 sessions, my session was in the top 20 of votes received.  Thanks to everyone who voted for me, I have some big plans.

Here’s the abstract for my session.  If you have any suggestions I certainly want to hear them.

Advanced Media techniques in Silverlight 3
Corey Schuman - ( Area: SilverLight / WPF Level: 200)

This session dives deep into media (Video, Audio, Images, and DeepZoom) in Silverlight 3. Choosing the correct video encoding, effectively using Adaptive Streaming, working with the Bitmap API, Pixel shaders, and tuning performance for media are topics covered in this session.

8 Essential Applications

The past couple weeks I got two new computers.  That means twice the installation fun.  On my main computer, I installed Windows 7 Beta along with all my apps.  Once everything was exactly the way I wanted it and it felt like home…the Blue Screen of Death.  Not once, but five times in three days.  (You would think that after all this time Microsoft would have canned the BSD for a friendlier message).  Needless to say, I counted down the minutes until Windows 7 RC released.

The biggest win out of this process was centralized data.  Going through so many installations it was essential to not rely on data being stored locally.  The below list of software saved the day for me:

1

image Live Mesh (link) - The number 1 must have program is Live Mesh (or an app/service similar to it).  Mesh has been totally non-intrusive and reliable.  The biggest compliment I can give Live Mesh is "the app acts like it isn’t even there".  After loading Mesh on my system, I can grab some coffee, and come back to a system that was exactly like it was before.

2

image Evernote (link) - Much like Live Mesh, Evernote non-intrusively backs up everything you give it.  People have been preaching Evernote for a long time, but  it is only recently that I have showed it love.  And, unlike many applications, Evernote has showed me love back.  (Now, in my spare time I need to program Mason’s Evernote idea and retire early).

3

Windows Live Writer (link) - Hands down the best blogging software.  I truly wish they made a Mac version.  If you do install Live Writer, take note, you will be prompted to install a number of other Microsoft Live pieces of software.  I get why they do it, but when I install a piece of software, I only what I ask for.

4

image Virtual Clone Drive (link) - another must have.  I have a number of ISO’s that I needed installed.  Virtual Clone enables you to mount the ISOs like a CD/DVD ROM.  Other than the the required reboot, Virtual Clone has saved me countless hours burning CD/DVD’s.

5

image Tortoise SVN (link) - The best SVN client for Windows.  I also like VisualSVN for Visual Studio.  There is a little disconnect between Tortoise and VisualSVN.  Tortoise wants to check in the whole directory and VisualSVN want to check only the necessary project files.  There are kinks between the two, but all in all Tortoise is a must.

6

image Pidgin (link) - The only IM client you’ll need.  I’m sick and tired of AIM, MSN, Yahoo Messenger loading their ads and welcome screens.  Pidgin gives it to you straight.

7

Development tools – The standard development tools for Silverlight development

8

Mac software – I work on a beautiful 17inch MBP.  There are always two apps that must be installed on a Mac (besides above apps) InstantShot and MainMenu.  InstantShot is a free screen grab utility and MainMenu is an automated maintenance application.

Loser

image Jungle Disk – I signed up for Jungle Disk expecting to use it as my main backup utility.  I wasn’t fond of the setup and I never got it to install on the Mac properly.  After backing up my data, which was terribly slow, I never reinstalled the application.  Now i have my one or two backups sitting in the

Visual Studio Tip: Open XAML in code only mode

A major annoyance of mine after installing a new instance of Visual Studio is when I open a XAML document for the first time. By default, Visual Studio opens a XAML document in Preview mode (the design on the top and the code view on the bottom).  As you start adding more and more elements to XAML, Visual Studio will render the XAML in the design view, even going as far as playing video back if you have a MediaElement defined.

The option to turn this off is buried in the Options.  The below figures show how to change the default behavior.

First click Tools > Options.

image

Then navigate to Text Editor > XAML > Miscellaneous.  Check the box that says Always open document in full XAML view.  If you don’t see Text Editor in the options menu, check the box Show all settings in the bottom left of the Options window.

image

Atlanta Silverlight Meetup

imageLast night was the first Atlanta Silverlight Meetup.  It was great meeting everyone and talking about the current state of Silverlight, architecture, design, and plans for future Silverlight Meetups.

The location was great, Gordon Biersch Brewery, and I would highly recommend the upstairs spot for a meetup.  Next month we’re moving up north to the Five Seasons Brewing Company

I would like to give a special Thank You to Glen Gordon for both helping promote the meetup as well as bringing some swag.  His support was and will be crucial for the success of this group.

‘Til next month…i bid you ado.

Announcement: IQ Interactive

imageThe past couple weeks have been extremely hectic work, writing, and…this: Next Monday I am joining the dev team at IQ Interactive.  It’s an exciting move because they’ve been traditionally excelled in Flash development.  Now they are venturing into the world of Silverlight.  Initially I’ll be working on an exciting, creative, and ground breaking Silverlight application (I wish I could say more, but mum is the word for right now).  Additionally, IQ is dedicated to becoming a major player in the Silverlight space.  I’m very excited to join the team and help make IQ a force to be reckoned with in Silverlight.

Every new beginning is accompanied with an end.  Leaving Schematic is my end, and I’m leaving with a heavy heart.  I can’t speak highly enough of my time with Schematic.  When I started, my goal was to learn as much as I could about Silverlight.  They provided me with all the opportunity to do so, and for that I’m eternally grateful. 

Finally, the people at Schematic are amazing.  Everyone I’ve worked with is at the top of their game producing amazing, high quality work.  I wish everyone the best of luck and hopefully we’ll work together again.