Friday, September 03, 2010    
Blog  
OpenLight Blog
Apr 4

Written by: Michael Washington
4/4/2010 4:46 PM 

image

In my article Silverlight MVVM File Manager, I showed how a designer could use the MVVM pattern to create a UI from scratch, without writing a line of code. However, while the run-time file manager looked like this:

image

The design-time file manager, in Expression Blend 4, looked like this:

image

Trust me, Blend can do much better. With the help of Mark Boulter, and Unni Ravindranathan at Microsoft, I figured out how to show design-time data in Blend when the data is using web services (without writing a line of code).

The Starting Project

You can get a copy of the project we will start with, at this link.

image

Unzip the project, and open it in Expression Blend. Open the MainPage.xaml file and click on LayoutRoot in the Objects and Timeline window.

image

Click on the Data tab…

image

You will see the Data Context section, and the commands and collections defined in the View Model. Now, normally Blend would instantiate this class and show design-time data on the design surface. This is way-cool.

image

The problem is, that the data in this case, is obtained by web services, and Blend is unable to call the web service in design-time so it just shows an error.

Create Sample Data

image

In the Data window, Click on the Create sample data icon.

image

Select Create Sample Data from Class…

image

Select the MainViewModel and click OK

image

You will now see the MainViewModelSampleData class.

image

Click and drag the SilverlightFolders collection (that is under the MainViewModelSampleData class), to the Objects and Timeline window.

image

… and drop it on the treeView

image

Click and drag the SilverlightFiles collection to the Objects and Timeline window and drop it on the [ListBox]

image

The sample design-time data will now show, but the format is now wrong. The reason is, that Blend created default templates for the collections we just dragged. We already have some templates, so we just need to tell Blend to use the ones we already created.

Note, had I used this method when I first created the tutorial this would not have been a problem because we would not have created a template at this point.

image

In Blend, in the Objects and Timeline window, right-click on the treeView and select Edit Additional Templates > Edit Generated Items (Item Template) > Apply Resource > SilverlightFolderTemplate.

image

Do the same for [ListBox] and select SilverlightFileTemplate

image

Now we have the design-time experience we desire. For this simple file manager, this may not be that big of a deal, but what if you had a really complex application your were designing?

 

Designing Using Design-Time Data

Another thing I forgot to do in the original tutorial, was use a icon for the files in the file window. Let’s fix that now.

image

In Blend, in the Objects and Timeline window, right-click on the [ListBox] control and select Edit Additional Templates > Edit Generated Items (Item Template) > Edit Current.

image

In the Projects window, drag the page_white.png graphic…

image

To the design surface. Drop it anywhere but inside the blue box that will appear.

image

If you look at the Objects and Timeline window (while still holding the mouse button down), you will see that the [StackPanel] is highlighted when you are not hovering over the blue box (on the design surface), but the [HyperlinkButton] is when you hover inside the blue box.

If you dropped the icon while [HyperlinkButton] was highlighted, it would bind the [HyperlinkButton] to the graphic. We don’t want that, the [HyperlinkButton] is already bound to the name of the file.

image 

Drop the icon outside the blue box and it will now appear inside the blue box (I know, confusing isn’t it, but cut the programmers at Microsoft some slack here. Designing a tool that allows all this magic with no code is hard to design…).

image

In the Objects and Timeline window, you will see the [image] below the [HyperlinkButton]

image

Drag it so that it is above the [HyperlinkButton]

image

Click on the [StackPanel] in the Objects and Timeline window, and in the Properties window, set the Orientation to Horizontal.

image

The design-time view shows us exactly what we want.

image

Click the Return Scope icon, in the Objects and Timeline window, to return to normal design mode.

image

The Objects and Timeline window will return to the normal project scope.

Hit F5 to run the project…

image

We now have the complete application.

The Revolution Continues…

The sample data in Expression Blend can also be used to allow a Designer to start a project in Blend and create the UI first. A Programmer could come in later and actually make it work. While it is best to:

  1. Gather requirements
  2. Create SketchFlows in Expression Blend to prototype the project
  3. Create the Model and the View Model
  4. Create the UI (the View)

Sometimes you may want to allow the Designer the ability to work on some UI before the back-end code is ready.

You can download the complete project at this link.

Tags:

5 comment(s) so far...

Re: Silverlight MVVM: Enabling Design-Time Data in Expression Blend When Using Web Services

Brilliant, Design time data is SO very important to a Designer, without it, you are just banging your hard against the wall!

Microsoft can have all the slack in the world, as long as I don't have to write code... :-)

Thanks a lot for the blog tutorial Michael,

Alan

By Alan Beasley on   4/10/2010 4:58 AM

Re: Silverlight MVVM: Enabling Design-Time Data in Expression Blend When Using Web Services

I've tried several of the Silverlight MVVM projects from Michael et al, but keep getting "The provided URI scheme 'https' is invalid; expected 'http'" error as at
private class WebServiceSoapClientChannel : ChannelBase, MVMFileManager.FileManager.WebServiceSoap {

public WebServiceSoapClientChannel(System.ServiceModel.ClientBase client) :
base(client) {
}
in this project. How to resolve?

By David Pressman on   5/21/2010 1:25 PM

Re: Silverlight MVVM: Enabling Design-Time Data in Expression Blend When Using Web Services

@David Pressman - You are trying to run the MVVM File Manager project on a site that uses httpS://. It only works with a site running http://

By Michael Washington on   5/21/2010 1:26 PM

Re: Silverlight MVVM: Enabling Design-Time Data in Expression Blend When Using Web Services

1. So what is the proper way to TEST this model/viewmodel before you make the view? My guess is you just make a test XAML page yes? Or perhaps you could generate unit tests?

2. Best tutorial Ive found so far. Desperately trying to understand MVVM so I can get started with it.. Bouncing between MVVM and MVVMLight

By matt herb on   6/22/2010 9:24 AM

Re: Silverlight MVVM: Enabling Design-Time Data in Expression Blend When Using Web Services

@matt herb -
I cover testing here: www.codeproject.com/KB/silverlight/SMVVMUnitTest.aspx

By Michael Washington on   6/22/2010 9:25 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use