Page view counter

November 2009 - Posts

A Party Game.

Quick Bit

David @Pogue tweeted a link to this NY Times article, reporting that there are [longer] better, faster [stronger] apps available than Shazam (which is about to start charging new midomilogo customers…) so I extensively examined the alternatives for just over  30 seconds,  and bought midomi (get it? Do, Re, Mi:  Mi Do Mi). 

It is faster than  Shazam, and offers more features  but most important you can either type in or, much better, sing or hum the song you are looking for.

After spending about an hour at 3am singing old pop songs in a key entirely my own, I invented the following party game (Sing That Tune) that I give to you in celebration of Silverlight 4 (though it has absolutely nothing to do with Silverlight). If it catches on, remember where you heard it!

  1. Player 1 (Chooser)  tells Player 2 (Singer) the name of a song.
  2. Player 2 sings/hums/etc. into midomi and the results are scored

[Those who require order and the illusion of fairness might decide to rotate the chooser role, and determine the singer role stochastically: perhaps using a die.] 

Scoring:

If midomi finds the song, Singer  scores 3 points if it is the #1 find, 2 points if it is in position #2, otherwise 1 point.

If midomi does not find the song Singer has the option to Challenge. If there is a challenge, the Chooser types in either the name of the song or the artist and has one attempt to find the song in midomi.  If the song is found, Singer loses 3 points, otherwise Chooser loses 5 points

[Could be a fun Silverlight application now that we have microphone support – but the song recognition may be non-trivial)

Posted by jesseliberty | with no comments
Filed under: ,

Silverlight 4 Fluid UI

MiniTutorial

This is the first of many mini-tutorials on Silverlight 4 features.

An enhancement has been made that allows you to add animation when items are added ,or removed from, the list box.  Silverlight 4 adds a new StateGroup to the ListBoxItem class:

ListBoxItemAttributes

 

A Simple Demo

To demonstrate this, I’ve created a wicked simple demo: the userfluidUIDemo types into the text box, clicks the add button and the text “floats” up into the list.

I begin with a simple Silverlight 4 program in Visual Studio 2010. The grid consists of two rows, whose height is set to the ratio of 5:1.

The top row takes a ListBox and the bottom row a stack panel holding a TextBox and a Button.

The Button’s event handler takes the text from the TextBox and adds it to the ListBox:

   1:  void Add_Click( object sender, RoutedEventArgs e )
   2:  {
   3:     if ( !String.IsNullOrEmpty( StringEntry.Text ) )
   4:     {
   5:        ListOfString.Items.Add( StringEntry.Text );
   6:        StringEntry.Text = string.Empty;
   7:     }  // end if
   8:  }     // end event handler

So Where Is the Magic??

To take advantage of the new FluidUI all you need to do is to set the behavior you want on any or all of the three new Visual States in the LayoutStates group of the ListBoxItem class.

The easiest way to do this is to open the project in Blend and right click on the ListBox. Choose Edit Additional Templates –> Edit Generated Item Container –> Edit a Copy

Name the new template and then click on the ListItem and open the CreateStyleStates tab. For this simple demo, I only modified the Loaded state.

To do so, click on the Loaded state and then expand the timeline. You’ll set new values for the entire Grid (which has 4 sub-elements). 

select 0 seconds, and set the opacity to 0 and the  y coordinate of a translate-transform to 200.   Click save keyframe values, and then select 1 second and set the opacity to 1 and the y coordinate to 0. Save the new template.

To connect the new template to the ListBox,, open the Xaml file and add a Style property to the ListBox control (as shown on line 7 below)

   1:  <ListBox x:Name="ListOfString"
   2:     HorizontalAlignment="Stretch"
   3:     VerticalAlignment="Stretch"
   4:     Grid.Row="0"
   5:     Width="225"
   6:     Margin="5"
   7:     ItemContainerStyle="{StaticResource ListBoxItemStyle1}" />

 

Build and run the program; when you add the text string to the ListBox.Items collection the loaded state is set, your animation is run and the new value floats into place.

Source Code

MainPage.xaml

<UserControl x:Class="FluidUIDemo.MainPage"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d"
   xmlns:dataInput="clr-namespace:System.Windows.Control ;assembly=System.Windows.Controls.Data.Input">
<Grid x:Name="LayoutRoot"
       Width="250"
       Height="200">       
    <Grid.RowDefinitions>
            <RowDefinition Height="5*" />
            <RowDefinition Height="1*" />
         </Grid.RowDefinitions>
      <ListBox x:Name="ListOfString"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Grid.Row="0"
        Width="225"
        Margin="5"
        ItemContainerStyle="{StaticResource ListBoxItemStyle1}" />
      <StackPanel Orientation="Horizontal"
       Grid.Row="1"
       Margin="5"
       HorizontalAlignment="Stretch"
       VerticalAlignment="Stretch">       
            <TextBox x:Name="StringEntry"
               Height="24"
               TextWrapping="Wrap"
               Text=""
               FontFamily="Georgia"
               FontSize="14"
               Width="171"
               HorizontalAlignment="Left"
               VerticalAlignment="Bottom"
               Margin="5,0,0,0" />
            <Button Content="Add"
               x:Name="Add"
               Height="24"
               FontFamily="Georgia"
               FontSize="14"
               Width="51"
               HorizontalAlignment="Left"
               VerticalAlignment="Bottom"
               Margin="5,0,0,0"
               IsEnabled="False" />
         </StackPanel>
</Grid>
</UserControl>

 

MainPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace FluidUIDemo
{
   public partial class MainPage : UserControl
   {
      public MainPage()
      {
         InitializeComponent();
         Loaded += new RoutedEventHandler( MainPage_Loaded );
      }

      void MainPage_Loaded( object sender, RoutedEventArgs e )
      {
         Add.Click += new RoutedEventHandler(Add_Click);
         StringEntry.SelectionChanged += new RoutedEventHandler( StringEntry_SelectionChanged );
      }

      void StringEntry_SelectionChanged( object sender, RoutedEventArgs e )
      {
         Add.IsEnabled = !String.IsNullOrEmpty( StringEntry.Text );
      }

      void Add_Click( object sender, RoutedEventArgs e )
      {
         if ( !String.IsNullOrEmpty( StringEntry.Text ) )
         {
            ListOfString.Items.Add( StringEntry.Text );
            StringEntry.Text = string.Empty;
         }  // end if
      }     // end event handler
   }        // end class
}           // end namespace

App.xaml Excerpt

<VisualState x:Name="Loaded">
   <Storyboard>
      <DoubleAnimationUsingKeyFrames 
         Storyboard.TargetProperty=
"(UIElement.RenderTransform).
(TransformGroup.Children)[3].
(TranslateTransform.Y)"
Storyboard.TargetName="grid"> <EasingDoubleKeyFrame KeyTime="0" Value="200" /> <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" /> </DoubleAnimationUsingKeyFrames> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> <EasingDoubleKeyFrame KeyTime="0" Value="0" /> <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1" /> </DoubleAnimationUsingKeyFrames> </Storyboard> </VisualState>
 

The Silverlight HVP Spec

The Silverlight HyperVideo Player: A Community Project

MiniTutorial

The Silverlight HVP Elevator Pitch:

The Silverlight  HyperVideo Player (HVP) will do for Silverlight How Do I Videos what Hypertext did for the Internet: it will provide simple, fast, and easy to use links to additional, supplementary or related information. 

As an example, the user might be watching a video on data binding and the term “Dependency Property” is mentioned and at the same time a link appears. If the user clicks on that link, the video is paused and an explanation of Dependency Properties is provided.

Update 11/20: We have received our MS-PL allowing me to move this project to CodePlex. I will update again with a link once it is in place.   Other updates in-line in red.

 

Summary of Purpose

While I have covered HyperVideo in numerous blog postings and videos in the past, this project will follow the creation and delivery of a complete application.  spinning out mini-tutorials and videos on an extremely wide range of topics.

The project will be designed, developed and delivered in collaboration with many others, both inside of Microsoft and out. 

Scope and Deliverables

This document will serve as the draft  “spec” for the first phases of development. I have intentionally under-documented the spec because of the immutable law that the likelihood of a spec being followed is inversely proportional to its size and (b) the requirements and design will evolve as the community adds ideas and refinement. 

Contributing to the Spec and to the Project

Contributions, refinements and critiques of this document are not only welcome, they are its lifeblood.  For now, I’ll ask that you respond in the comments, , as things evolve I’ll roll out a better mechanism for tracking all of the feedback.

Contributions to the project will be described by the end of the month; for now, if the comments section is insufficient, please feel free to write to me directly.

Core Functionality

The Silverlight HVP will be a modular, extensible, and robust Rich Internet Application that can deliver HyperVideo on any platform that is supported by Silverlight.  Initial requirements include:

  • Present any existing Silverlight How Do I Videos as is.
    • That is, the Silverilght HVP will be backwards compatible with the existing player
    • Any existing video can be enhanced to provide a Table of Contents and/or Hyperlinks either through encoding or marker injection at which point it will be indistinguishable from videos created specifically for the Silverlight HVP
  • TOC TreeDisplay a Table of Contents (TOC) for each video, with affordances for users to move from one topic to the next
    • Current plan is to separate the TOC from the links, presenting the complete Table of Contents when the video begins, but revealing the links as their context arises. 
    • For sufficiently complex Tables of Content we may want to provide the ability to collapse and expand subtopics
  • Display a list of topics as they are mentioned in the video, with each topic’s name acting as a HyperVideo Link.
  • Full support for Closed Captioning and all standard video controls (e.g.,, stop, pause, skip etc.)
  • TOC and Hypervideo Links will be decoupled from the markers in the video so that the links can be modified without changing or re-encoding the video

Potential Features

There are literally dozens of potential additional features, some of which are key to adoption of the player in various settings; others either won’t make it into early versions of the product or will be developed by the community once the open source mechanism is in place.

[Images in this spec are for illustration purposes and are not final visualizations of the application]

Not Just An Application

A primary goal of this project is not only the end product, but the opportunity for discussing Silverlight skills in the context of a real-world application.  Building the Silverlight HyperVideo Player will force consideration of a number of interesting aspects of Silverlight and related technologies.

Some Thoughts on Implementation

There are numerous ways to add markers to video in Silverlight; in the course of developing the Silverlight HVP we will consider two.  The easier approach is to use Expression Encoder to do the initial heavy lifting.  Encoder is designed to record four pieces of information about each marker, the first two of which matter to us.

  1. MarkersThe elapsed time into the video at which the marker appears
  2. A “value” that can have an intrinsic meaning or not as you choose (more about this below)

The other two pieces of meta-data in a marker are the thumbnail and keyframe for each marker; these are typically used to create and manage “chapters” in long videos.

Working With Markers

The Media Element (or a control derived from the Media Element) will fire an event each time a marker is encountered. The HyperVideo player will respond to that event by following the link or opening the associated video as indicated in the metadata that relates a given marker to a given action.

During development of the Silverlight HVP we will explore at least two ways of managing the metadata: storing it in a relational database and storing it in an XML file.

As an off-the-cuff example, the MetaData might be stored in an XML file that looks more or less like this:

<VidepPlayerMetaData>
   <VideoInfo>
     <Title="Data Binding" />
     <URL="http://foo.bar.com" />
   </VideoInfo>
   <Markers>
      <Marker Val="DBX09-001" Action="Play" URL="http://silverlight.net/learn/videos/v1027" />
      <Marker Val="DBX09-002" Action="Play" URL="http://silverlight.net/learn/videos/h1375" />
      <Marker Val="DBX09-003" Action="Link" URL="http://en.wikipedia.org/LCA" />
      <Marker Val="DBX09-004"  />
      <Marker Val="DBX09-005" Action="Play" URL="http://silverlight.net/learn/videos/v1288" />
   </Markers>
</VidepPlayerMetaData>

 

Encoder can create a working player to get us started.

At its heart, the player is a control or a set of controls that can be skinned to change its appearance and can be subclassed to change its behavior. Doing so demonstrates best practices, and significantly simplifies the development effort.

The UI Sketch

Of course, the player created by Encoder is just the starting point it will serve as an essential SilverlightHVPcomponent within a larger application.  The UI design at this point is only a first approximation, subject to significant change, but the rough sketch is enough for me to get started on development.

“You start coding, I’ll go find out what they want.”

The sketch calls for four windows and a menu.

  • The primary player (top center)
  • The Table of Contents (top left)
  • The Links window (top right)
  • The linked-to (target) window (bottom)

Each window will  be resizable, movable, and dockable.

Deliverables and Due Dates:

It is tempting to say that too little is known to create a set of milestones and delivery dates; but projects with no dates have a known finish time: never

Been there, done that, still have the scars. 

Far better to stake yourself to a timetable, then be held accountable; if not to hitting the dates, then to explaining  what has changed that justifies a change.

Thus, after many seconds of careful consideration, here is the timeline for the first iteration of this project.

Task Due
This overview spec completed & published 11/15
Proof of concept deployed  on SilverlightGeek.me 1/1
Delivery of final UI Assets 1/21
First Release Mix10

My plans of course go well beyond Mix, but this is, I hope,  a good start.  I welcome and urge you to join me in an open technical discussion of any and all of this project.

The fine print:

  • Project Turing, and the feedback it engendered, served as a great testing ground for building this kind of application. It was not intended that Turing would be completed, and I’ll suspend it indefinitely.  It is my intent that this project will roll out as an application.
  • I believe this project has tremendous potential, and I’m committed to it, but I’ve been on enough great projects that were stopped before seeing the light of day, that I make these commitments knowing that not everything is under my control (the great delusion is I still think anything is under my control)

On Our Cross Plat Commitment

When I joined Microsoft and started talking about Silverlight, many in the Mac community expressed skepticism about Microsoft’s long-term commitment to the Mac platform. In its most rabid form, the concern was that we were supporting the Mac only preemptively and would drop our support for the Mac as soon as enough Mac developers embraced it (!)

So, 2.5 years later, with the worst not having happened, I have renewed my personal pledge to make sure, to the best of my ability, that Silverlight not only continues to work on the Mac but looks and feels like a Mac app. 

Since I love the Mac, and I love Win7, and I really wanted a Working Laptop2 single machine to walk around with, I recently invested (personally) in a MacBook Pro and put enough RAM in it to happily run both Operating Systems simultaneously.  Here it is at my office with a 24” Cinema display, running both operating systems and pleasing me no end.

I absolutely understand company loyalty (and loving the Mac is not disloyal to Microsoft) but I tend to believe that we do best in recognizing the strengths of our allies and our competition. To quote from a Daft Punk: *

Work it, make it, do it, makes us
Harder, better, faster, stronger

Very much looking forward to PDC – look for me in the The Big Room or check for me on Twitter: @JesseLiberty

 

 

* In the original posting I attributed these words to Kanye West. This was a happy mistake, as your corrections led me to the music of Daft Punk

Posted by jesseliberty | 9 comment(s)
Filed under: ,

Commitments

I’ve received a number of messages along the lines of, “Seems like things have really slowed down on your blog” or even “Hey, where’s the signal? Too much noise.”

Fair enough; though this is a common effect of the run up to a major “show” such as PDC (I get caught up in creating material that won’t be released until after the show and things go a bit wobbly here) vacation’s over! Time to Party.

So here are my commitments to you:

In the coming weeks you’ll see:

  1. A series of new and better videos – look for the first ones during PDC.

  2. At least two Silverlight From Zero  mini-tutorials per month, and at least two Intermediate to Advanced mini-tutorials each month. ecqg50Pct

  3. Revitalization of the “Example Code Quality Guarantee” page noting the development environment for all demos.

  4. The HyperVideo Project Spec will be complete by  November 23 (if you wish to contribute to the spec, please send email to jliberty@microsoft.com ) and Coding begins for the  HyperVideo Project Tuesday November 24.

Thanks

jessesig

Subscribe to 62 Silverlight Twitter’ers.

I’ve created a list that currently follows 62 top-notch Silverlight Twitter participants. twitterBirdSidebarThe list will grow (and be culled) over time, but you can easily sign up to receive tweets from everyone on the list just by clicking on this link and subscribing to the list as a whole. 

 

icon-rss


While you are at it, please take a moment and subscribe to my tweets and to this blog

Designer v Xaml v Code

MiniTutorialLogo2

This is the second in my new series on Getting Started With Silverlight (please see the first article for information on the series and where to get the software you need).

[updated11/8]

Don’t Start with Xaml…

Until recently, just about every introduction to Silverlight started out by talking about Xaml; the markup language of Silverlight, WPF and Workflow. I believe it is time to stop.

A quick note on Xaml – we all agree how to pronounce it: zam-el to rhyme with camel, but not on how to spell it; most folks use upper case, but  Rob Relyea told me in private correspondence that Xaml was released via MS Open Specification Promise, and prefers it aesthetically, even if he does conform and write XAML like most others programmers. But I’m starting a movement.

(Apologies to Rob and his brother Dave for initially pointing to the latter when I meant the former and confusing MS OSP with ECMA, for which I should be flogged.)

Teaching Xaml first made sense when Xaml was the only (or best) way to create controls. But with a working design surface, Xaml is too high a bar to set just to get started.

Many development environments have gone though a similar evolution: start with HTML, then get a WYSIWYGIYAVL (What You See Is What You Get If You Are Very Lucky) editor.  ASP.NET by hand, then ASP.NET from the toolbox. Etc.

I recommend that if you do not know Xaml you not try to learn it (at first) as you can go very far without doing so, it can be a stumbling block, and the tool will provide incredible help for learning the markup as you need it.

I may be the first person to write down that advice. (I may be stoned to death.) But I suspect I won’t be the last.

Silverlight Without Xaml

To get started, open VS2010 and create a new Silverlight project (I use C# but feel free to use any supported language).  Let’s name it ThreeApproaches

When Visual Studio settles down you’ll probably see a split window with the designer on top and Xaml on the bottom.  Let’s close the Xaml by clicking on the “make top window the only window” button on the far right of the splitter bar

CollapsePane

Open the Toolbox if it is not already open ( Ctrl-W X) and pin it in place.  Then click on the Grid that is the default layout control.  When you click on it, margins will appear and putting the cursor into the top or left margin will offer you a preview of where you might click to create columns or rows respectively. Go ahead and create two rows and two columns, and then shrink the entire grid down to small enough to look ok.

Add Your First Control

Drag a Textblock out of the toolbox and place it more or less in the upper left box and then open the properties window. If the TextBlock’s properties are not displayed, click on the TextBlock to make it the selected control.

Somewhat unusually you set the name of the control at the very top of the NameProperty Properties window

Notice that there are two tabs: Properties and events. Make sure you have Properties selected, and below that you may want to click on the Categorized button rather than the A-Z to make this a bit easier to follow.

Expand the Layout property and by clicking in the black triangle next to both Height and Width (and clicking Reset) you can set the TextBlock’s dimensions to be set in accordance with whatever string (characters) are begin displayed. Set Layoutthe remaining values as shown in the next image, and the TextBlock should  show in the designer as placed in the upper right hand box, 5 pixels from the right and bottom margins.

(To save space I cut out some rows, but you can leave those set to their default values).

Wasn’t that cool? No Xaml needed. But if you want to learn Xaml, aha! there are two great features to help. First, click on the horizontal split button on the far right of the design window. This will restore the split window you started out with.HorizontalSplit

Notice that the Xaml is now shown. Scroll down to line 20 and you should see the definition of the TextBlock, now in Xaml. Notice the 1:1 correspondence with the properties you’ve set.

XamlToProperties 
(click on image for full size)

Xaml and Intellisense

Let’s write the second control (TextBox) in Xaml. Click into the Xaml window, XamlIntellisenseand below the TextBlock, type an open angle brace. Intellisense immediately springs forward offering to help you pick the control you want. The more you type, the more Intellisense will narrow in on your choice.

Once you select TextBox, and hit the space bar, again Intellisense jumps in, this time offering suggestions as to the properties you might want to set.

Fill in the property / value pairs as shown below,

   1:      <TextBox Name="Name"
   2:               HorizontalAlignment="Left"
   3:               VerticalAlignment="Bottom"
   4:               Height="25"
   5:               Width="75"
   6:               FontFamily="Georgia"
   7:               FontSize="14"
   8:               Grid.Row="0"
   9:               Grid.Column="1"
  10:               Margin="5" />

Your TextBox will appear in the upper right corner of the designer, with all the properties set to correspond with what you’ve written in the Xaml.

To make the TextBlock  (the first control) consistent with this, click on it in theSetTheTextProperty designer, and scroll down to Text Category and drop down the FontFamily to pick “Georgia.”

Set the size to 14 and click the Beer bold button to set it to Bold.

Return to the Text property (above Layout) and change it from TextBlock to Name? and make sure that the margin is set to 5 in the Layout section.

When all of that is done, hit Control-F5 to run the application and you should see a prompt and a textBox into which you can enter your name.RunThe Program

If you like, you can click in the grid but not on one of the two controls and bring up the properties for the Grid. Scroll down to, and expand, the “Other” category and click the checkbox next to “Show Grid Lines” to reveal the rows and columns you created.

Okay, now you know you can write your controls in Xaml, but why bother when you can just drag them onto the designer from the toolbox and set their properties in the Properties window.  I truly believe that the latter approach is faster, less error prone and generally a much better way to get started.

Will you want to hand-code Xaml eventually? Maybe, but my guess is less and less as you get better and better at Visual Studio and, eventually, Expression Blend.

Dynamic Creation of Controls in Code

There is a third way to create controls: dynamically in code.

You can stop reading right here. You won’t need to know this for a long time. I am putting this into this article because (a) this can be a powerful technique when you do need it and (b) for some folks understanding the relationship between dynamically (C#) and declaratively (Xaml) created versions of the same object can be very helpful in groking what Xaml is about.

But your mileage may vary.

It’s All Just Objects

Every object you create in Xaml can also be created at run time in code. To see this, let’s create a second set of prompt and TextBox that will appear when the project is run.

To do so, turn the expander next to MainPage.xaml to reveal the code behind page, MainPage.xaml.cs. (or MainPage.xaml.vb if you are working in VB).

In the constructor, we’ll put a handler for the Loaded event (the loaded event runs when the page is loaded) and we’ll do our work in the event handler Visual Studio creates. 

To do this, click into the constructor and type Loaded +=  then hit Tab twice to let Intellisense create your handler for you.  Click in the handler and delete the exception that Intellisense put there to remind you to implement the handler logic.

We’ll talk about events and event handlers in an upcoming mini-tutorial; but for now, you can ignore the details or feel free to experiment (you can’t break anything).

Creating The TextBlock Dynamically

As noted above, every control can be created as a CLR object, and again Intellisense will help enormously. Begin by instantiating a TextBlock. the Identifier you use (in this case AddressPrompt) will become the Name property.

   1:      void MainPage_Loaded( object sender, RoutedEventArgs e )
   2:      {
   3:        TextBlock AddressPrompt = new TextBlock();
   4:      }

You will now add each property to the instance of TextBlock, though here you must be sure to be type-safe. Let’s walk through it.

First, you’ll want to set the HorizontalAlignment, which turns out to be an HorizAlignmentenumerated constant. Again, Intellisense will help by offering the legitimate values

Fill in the Vertical Alignment in the same way.

When you try to fill in the Margin as a value, you’ll not the red squiggly line indicating something isMargin wrong. Hover over the Margin property and the tag will indicate the type of the Margin: Thickness. At this point you can open the help files to read about the Thickness type, or you can just instantiate one and see how that goes. I personally prefer the latter. Not onlyThickness Help does Intellisense show you that there are three possible constructors (which you can scroll through with the arrow keys) but it identifies the purpose of each parameter and guides you through filling them in. While I show the third constructor here, which lets you set the left, top, right and bottom margin, we’ll actually use the second constructor which lets you assign one value for all four.

Next we want to set the Height and Width. Hovering over each will reveal that they are doubles, but in this case we want to set them to “auto” – a quick check of the documentation reveals that this is accomplished by assigning the static value  Double.NAN – a flag for the compiler to set them automatically.

You can set the FontFamily and FontSize as a string and a double FontWeightsrespectively, but set the FontWeight using the enumeration.

 

 

 

 

 

 

 

 

Here is the code we have so far

   1:  TextBlock AddressPrompt = new TextBlock();
   2:        AddressPrompt.HorizontalAlignment = 
   3:            System.Windows.HorizontalAlignment.Left;
   4:        AddressPrompt.VerticalAlignment = 
   5:            System.Windows.VerticalAlignment.Bottom;
   6:        AddressPrompt.Margin = new Thickness( 5d );
   7:        AddressPrompt.Height = double.NaN;
   8:        AddressPrompt.Width = double.NaN;
   9:        AddressPrompt.FontFamily = 
  10:            new FontFamily( "Georgia" );
  11:        AddressPrompt.FontSize = 14d;
  12:        AddressPrompt.FontWeight = FontWeights.Bold;

 

Placing the Control into the Grid

We placed the first controls into the Grid by writing

Grid.Row = "0"
Grid.Column = "1"

But of course, neither TextBlock nor TextBox has a Grid.Row property.  These GridSetColumnare Extended Properties, properties defined in Grid but borrowed by other elements to assist in their placement. The C# equivalent is to call the ??? methods of the Grid class, passing the UIElement (in this case, AddressPrompt) that you want to place in the grid, and then the column number and  row respectively.

Grid.SetColumn( AddressPrompt, 0 );
Grid.SetRow( AddressPrompt, 1 );

That done, the last step is to add the new element to the Grid itself, by referencing the Children collection of the particular Grid instance, and calling the Add method on that collection, passing in our Element:

LayoutRoot.Children.Add( AddressPrompt );

 

We can then follow a very similar process for the Address text box. The one interesting addition I’ll make is to set the color on the text in the TextBox. You do this by setting the Foreground property, and you must assign it a SolidColorBrush as its value. You can instantiate a SolidColorBrush by passing in a Color from the Colors enumeration,

AddressInput.Foreground = new SolidColorBrush( Colors.Blue );

When you look at the designer, you will not see either of these controls; they RunningProgramwon’t exist until the program runs. Press Control-F5 and try out your new program, however, and you’ll see that the dynamically instantiated controls are indistinguishable from the declarative (Xaml) controls; at least to all appearances:

You Can, But Don’t.

Even though dynamic declaration of elements takes many more lines of code; C# developers are often tempted to eschew Xaml and go with C# – after all; it is a tool they know. 

Xaml has many advantages, however, not least of which is that it is highly toolable.  That means that it works extremely well with the Visual Studio Designer and with Expression Blend, which in the long run means far faster development, better looking and easier to maintain applications, and a much easier interaction with designers.

The Source Code

For completeness, here is the Xaml file followed by the C# file:

MainPage.Xaml

<UserControl x:Class="ThreeApproaches.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White"
        Height="135"
        Width="250">
    <Grid.RowDefinitions>
      <RowDefinition Height="1*" />
      <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="1*" />
      <ColumnDefinition Width="2*" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.ColumnSpan="1"
               HorizontalAlignment="Right"
               Margin="5"
               Name="myFirstTextBlock"
               Text="Name?"
               VerticalAlignment="Bottom" />
    <TextBox Name="Name"
             HorizontalAlignment="Left"
             VerticalAlignment="Bottom"
             Height="25"
             Width="75"
             FontFamily="Georgia"
             FontSize="14"
             Grid.Row="0"
             Grid.Column="1"
             Margin="5" />
  </Grid>
</UserControl>

[Note that I cleaned up the Grid columns and rows, using relative sizing (1*) and making the relative sizes of the columns 1:2 – all of this to be explained in an upcoming Mini-tutorial]

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace ThreeApproaches
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      Loaded += new RoutedEventHandler( MainPage_Loaded );
    }
 
    void MainPage_Loaded( object sender, RoutedEventArgs e )
    {
      TextBlock AddressPrompt = new TextBlock();
      AddressPrompt.HorizontalAlignment = 
        System.Windows.HorizontalAlignment.Left;
      AddressPrompt.VerticalAlignment = 
        System.Windows.VerticalAlignment.Bottom;
      AddressPrompt.Margin = new Thickness( 5d );
      AddressPrompt.Height = double.NaN;
      AddressPrompt.Width = double.NaN;
      AddressPrompt.FontFamily = 
        new FontFamily( "Georgia" );
      AddressPrompt.FontSize = 14d;
      AddressPrompt.FontWeight = 
        FontWeights.Bold;
      AddressPrompt.Text = "Address ?";
      Grid.SetRow( AddressPrompt, 1 );
      Grid.SetColumn( AddressPrompt, 0 );
      LayoutRoot.Children.Add( AddressPrompt );
 
      TextBox AddressInput = new TextBox();
      AddressInput.HorizontalAlignment = 
        System.Windows.HorizontalAlignment.Left;
      AddressInput.VerticalAlignment = 
        System.Windows.VerticalAlignment.Bottom;
      AddressInput.Margin = new Thickness( 5d );
      AddressInput.FontFamily = 
        new FontFamily( "Georgia" );
      AddressInput.FontSize = 14d;
      AddressInput.Foreground = 
        new SolidColorBrush( Colors.Blue );
      AddressInput.Width = 100d;
      AddressInput.Height = 25d;
      Grid.SetColumn( AddressInput, 1 );
      Grid.SetRow( AddressInput, 1 );
      LayoutRoot.Children.Add( AddressInput );
    }
  }
}

 

Next in this series: Creating Input Forms with the Silverlight Toolkit

(Note, when the article is posted, the name of the next posting will become a link)

Silverlight From Zero

MiniTutorialLogo2

This is the first of a series of blog posts directed at folks who are new to Silverlight and/or want to brush up on the fundamentals.  It will consist of mini-tutorials on topics I think need more coverage, and links to existing mini-tutorials, tutorials and videos. We begin… at the beginning.


What is Silverlight, Why Do I care?

This is the traditional place to start, but in all probability if you are here, you know the answer. In brief: Silverlight is Microsoft’s Rich Internet Application enabling technology. The goal is to create applications that are delivered by the browser but which offer the user experience of a desktop application.  There is no question that using a RIA technology offers the ability to create a quality of experience that cannot be matched by more traditional approaches.


Where Do I Get It?

Everything you need to start writing Silverlight applications is available on our Get Started page.WebPlatform

If you don’t already have Visual Studio, consider starting with Visual Web Developer (VWD). You can download both VWD and Silverlight together using the Web Platform Installer

Otherwise,  you may want to take a look at the more powerful  Visual Studio 2008 or the new beta of Visual Studio 2010.  Once you get serious with Silverlight you’ll really want  Expression Blend.  


Navigating the Get Started Page

On Get Started you’ll find an array of options. The first option tells you how to get Visual Studio or VWD, and options 2, 3 and 5 while incredibly useful, are not required for your initial foray into Silverlight… so just grab option 4.

Quick Tour?StartLearningAbridged

In the next section on the Get Started page, you’ll find a quick tour of Silverlight.  

This is a great way to get going. On the other hand, if you are going to follow this series, you may want to watch just #1 (Getting Started video) and hold off on the rest as I’m  going to integrate all of that material (including Tim’s terrific 8 part blog series) as we go.

The Tools

Tim’s excellent first video in his series will definitely get you started on the right foot using the tools.  TimH

I’ve never fully understood why developers don’t spend more time learning the details of Visual Studio; it is, after all, our fundamental tool, the environment in which we live all day. Time spent on getting all you can out of Visual Studio will pay dividends for a long time.

Diving In

With that, you are ready to dive into Silverlight. Arguably, the best way to get a handle on what Silverlight is, is to write a program that uses it to accomplish something reasonably useful. A good starter video on building an application (a bit old, but still correct) is this gentle intro to creating a Silverlight application with Visual Studio, using the Canvas to lay out controls.  Then read Part 2 in Tim’s series on layout.

Next in this series: Three Approaches: Designer, Xaml or Dynamic