Page view counter

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

 

Silverlight Synchronicity

SilverlightConsulting

At Mix ‘07 I watched Scott Guthrie’s keynote introduction of Silverlight,  and it was a watershed moment in my career.  Within 48 hours I had pivoted my consulting business to focus entirely on this new and game-changing  technology; a few months later I ended 15 years as an independent to join Microsoft as Silverlight Geek, a job that has been immensely rewarding.

On a personal (and seemingly unrelated) note; while my older daughter was in high school she became fluent in American Sign Language (ASL) under the tutelage of two terrific teachers, one hearing, one deaf. She joined the school’s justifiably famous Pocket Players bi-lingual (ASL and spoken English) theater group under the  direction of the incomparable and greatly RobinWoodmissed Robin Wood, and the extraordinary  natural-born-teacher Ron Galiazzo, (who has been patient  enough to teach me a little faltering ASL as well). My daughter learned, and taught us, great respect for Deaf culture,  and I had the incredible pleasure of watching her perform and then taking part with her in a celebration of Chinese American Deaf culture in Boston, where I spent the evening talking happily in broken-ASL with my tablemates.

The third piece of yesterday’s synchronicity touches on the person who most famously embodied the stereotype-shattering new awareness of all that the hearing world has historically gotten wrong about Deaf culture, and the abilities and supposed limitations of deaf people, Marlee Matlin. Her singular and award winning performances in Children of  A Lesser God and The West Wing have had an impact on me and so many others,  for over twenty years.

All of this came together spectacularly yesterday when I posted a small blog entry on the ability of Silverlight to support closed captioning and my commitment to including Accessibility in the design of the HyperVideo project.MatlinTweet This was picked up, I’m incredibly happy to say, by Ms. Matlin who tweeted about it to the National Association of the Deaf  (NAD) and “everyone.”  I have to say, that not only made my day (week, month!) but brought home how much impact Silverlight can have. 

I don’t want in any way to exploit Ms. Matlin’s tweet; this is not about promoting our technology as much as enjoying having made indirect contact with someone whose work I admire, and sharing that joy with the Silverlight community. It was very cool to see what she wrote, but it is also a reminder how vital it is, to get these things “right,” including remembering that adding Accessibility isn’t a trivial decision; but one that affects the ability of a great many people to use the products that Silverlight enables. 

Hypervideo, Closed Captions & Accessibility

 

 

The Closed Captioning For Silverlight Media page at T2Sami.com says in its commentary: 

A number of very sophisticated Silverlight Video players are under development: some open source, some proprietary…. The commitment to captioning in Silverlight is not so well supported. If it is included at all, it is frequently an afterthought.

In my initial series on HyperVideo I started with the media player emitted by Encoder, and stripped out the Closed Captioning button. That was unnecessary and I regret it. Please take this posting as my pledge not to have closed captioning and other accessibility issues be an afterthought; but rather they will be an integral part of the spec I will deliver for HyperVideo on December 1 for the renewed HyperVideo project.

In the  interim, please take a look at this excellent tutorial on Captioning In Silverlight on the Accessible Media Project and also be sure to check out these two open source projects:

Expectations Are Everything

Quick Bit

Anyone who has ever debugged a program knows that you tend to see what you expect. I’ve learned that lesson a dozen times, in a dozen ways. 

A classic example is to ask someone to read the following illustration aloud (don’t over-think it but when you’re sure you’re done, see the answer here).

 

ParisInTheSpring

 

 

 

 

 

 

Expectations


Today, I got smacked in the face with the power of expectations.  One of the best dog trainers I ever met recently recommended a new kind of dog harness to us; saying “you never tug or pull the dog; you just walk and it makes it more comfortable for him to come with you than to do anything else.”

So, we plopped down our $20 for a Gentle Leader Easy Walk harness, and then it sat in the mudroom  HarnessOnDog for three weeks before I finally got around to trying it.  I put it on and – wow! what a difference. Just like Jen said, no pulling, no tugging, I didn’t have to “correct” him at all (I hate being corrected, myself).  It was really quite impressive until I got home and took it off him and found I’d accidentally attached his lead to his traditional collar, and the new harness had not been at work at all.

Lesson learned? For about 5 minutes.

 

 

PS: Jen is also the trainer who told me “A tired dog is a good dog, and an exhausted dog is a great dog.”  For two ultra-short movies of Charlie please see here or here.

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

Building A Product For Real

As some of you know; I’ve been showing a number of Silverlight features in terms of Project Turing.  It was never my intention to finish that project; rather to use it as a mooring point to ensure that the mini-tutorials were realistic about building applications.

The second purpose of the Turing project was as a proof of concept. I now have the green light to go for a full development process, with the clear goal of creating a working application that will be:

  • A complete design to delivery project
  • A source of frequent mini-tutorials and videos
  • An opportunity for much more intense community contribution
  • An opportunity to work with designers and the Expression team
  • A product we can put to use here on Silverlight.net

It just doesn’t get any better than that. 

Who, What, When, Where and Why

Who:  If all goes as planned, this project will be moderated open-source with active participation and contributions from as many in the community as care to participate. Until now, I’ve been doing what I refer to as “glass house” programming; that is, designing and programming myself, but openly and documented.  It is possible that the initial stages will be glass house, but the plan of record is to move to Open Source.

What: The VideoWiki project lends itself to being completed in two discrete phases.  Phase 1 will be the creation of a Silverlight HyperVideo player targeted at enhanced How-Do-I videos. Phase 2, moving from HyperVideo to VideoWiki will follow if time and interest allows.

When:  While I already have a number of postings on HyperVideo, it is time to re-think, redesign and to create a detailed specification.  I’ll deliver that, here on my blog, by November 30. 

Design and Coding begin December 1, 2009 and will be completed by July 1, 2010.

Where: For now the project will live on my blog; but if this is to be truly open source then it must move to CodePlex. That is the goal.

Why:  To meet the needs of both novice and intermediate Silverlight programmers, and if Silverlight 4 comes along during our development time, to provide a great basis for introducing new features.

 

It shouldn’t be all that hard

The truth is that this is not all that big a project, and a quick look at the work done last year reveals that the tools will do a lot of the work for us. That said, there are some interesting subtleties we should be able to explore along the way.

If you have early thoughts about what should/must/must-not be in the project, be sure to let me know.

With PDC on the way, there will be a short-term drop in the release of videos and mini-tutorials for the next couple weeks., though I will make sure that there are some very substantive posts during that period. Expect a sharp uptick afterwards.

Time To Update My Favorites

Periodically I take a shot at listing my favorites: some folks find useful items they hadn’t looked at and it forces me to take a long hard look at whether it is time to try something new.

Today: Favorite Utilities

Business success team

Upcoming:

  • Favorite Mac Utilities
  • Favorite iPhone Apps
  • Favorite Programmer Utilities
  • Favorite Non-fiction
  • Favorite Fiction
  • Favorite Programming Books
  • Favorite Music (uh oh)

On the one hand, I can’t imagine why you’d care. On the other hand, building a community is more than just code; and sharing favorites can be more than just utilitarian.  In any case, you (no, really, you) are strongly encouraged, asked, pleaded with;  to comment, add your fave’s and etc.

Favorite Utilities 2009

NB: This list does not include my favorite programming utilities, and  is just those utilities that run on Win 7 (which is now on all my Win machines)

 

Amazon's Media Library - one of the better ways to share what you're reading and listening to. Dynamic, configurable, pretty easy to cope with.

Auto Hot Key – Finally! Add macros to a simple text file and they work everwhere.  I have two files: auto-correct (fixes common spelling errors instantly) and my macros. For example “@me” instantly changes to http://www.SilverlightGeek.me  The syntax is simple and adding or modifying a macro takes seconds.

ClipX – Very sweet, small, easy to use clipboard manager, with “yank-pop” and with reserved phrases you can reach easily.  Learning time: 2 min.

Evernote – Create (or clip) notes about anything, organize to the extent you want, find them instantly, access via PC, Mac, phone, web. Truly brilliant. Truly indispensable.  Uses include everything from meeting notes to taking a picture of your car and asking it to lead you back using the GPS. You can also ask “show me every note I wrote in the past 2 years when I was within 50 miles of right here” – great for biz travel.  See iPhone Utilities.

Exam Diff Pro - best I know for examining differences in files and directories.  More coverage under Programming utilities, but I also use it for comparing documents.

FinePrint - print pages 2-up and much more. Can save a lot of paper and a surprising amount of printing time. Installs as a printer.

GoToMyPC - access your pc securely from anywhere. Just great, though I may give it up if the built in functionality in Win7 is sufficient.

GoodSync – Easy, efficient backup or synchronization between computers. 

iMo Hulu Desktop – Changed the way I deal with TV. I’m not much of a TV watcher, but the ability to queue up shows, subscribe and watch on my PC (for which I highly recommend the iMo Pivot 7” USB screen) has meant watching the Daily Shows that I’d otherwise miss.

Hypersnap DX - My screen capture of choice for many years and I still really like it, but it is time to check out Snaggit combined with Paint.Net

Live Writer - Best blog authoring software I know of. Be sure to pick the best of the plugins.

Pandora - The Music Genome Project. Single best music site on the net, now on phones as well and I swear it is getting better. Be certain to find their technical background material and to explore how the service works. This is not “wisdom of the crowds” this is “quantify all you can about music and then find other music that is similar in meaningful ways. See this good intro from the NY Times.

Roboform – Individual passwords for every site, generates strong passwords, master password to protect them all. Easy, fast, effective, customizable and totally reliable. Now supports sharing across multiple PCs. Partial integration Stackswith 1Password (see Mac Utilities).

7Stacks – Nice small utility that lets you create a stack of links on your toolbar. In the image shown, my stack is linked to a folder; anything in the folder pops up in the stack. 

Trillian – All your IM Services in one place. I particularly like the ability to tie actions to events. I’ve set it up so that adding the word urgent to the text causes it to make a special sound. Very flexible.

Virtual Clone Drive Allows you to mount an ISO, BIN or other image file onto what appears to be a physical drive, but is actually a virtual drive. Very slick (and free!)

Bayesian Probability

 

Spent the early hours of this morning reading a great blog post by Jeff Atwood which makes reference to a Elizer S. Yudkowsky’s Intuitive Explanation of  Bayesian Probability. The timing was good as I had just finished the (highly recommended) book The Drunkard’s Walk which agreed with Jeff that most humans simply are not wired to deal with probability very well.

The Drunkard's Walk: How Randomness Rules Our Lives (Vintage) (9780307275172): Leonard Mlodinow: Books
ISBN: 0307275175
ISBN-13: 9780307275172

Yudkowsky poses the following canonical problem:

1% of women at age forty who participate in routine screening have breast cancer. 80% of women with breast cancer will get positive mammographies. 9.6% of women without breast cancer will also get positive mammographies. A woman in this age group had a positive mammography in a routine screening. What is the probability that she actually has breast cancer?

The frightening thing is that according to Atwood, only 15% of doctors get this right. And they’re off by a lot. That is, the average answer is in the range of 80% while the correct answer is 7.8%.  Apparently, there is something about the way we think about the problem that makes 7.8% hard to accept, and Yudkowsky does a great job of walking you through the logic in painfully small steps.

To me, however, there is a pretty straight-forward way to think about this (though it may only be intuitive since I’ve been through this a few times).

What Do We Know & What Does It Imply?

We have three pieces of information:

1% of sample are TRUE  (that is have cancer)

80% of sample who are TRUE will test TRUE

9.6% of sample who are FALSE will test TRUE.

On the face of it, we should guess that the percentage of women who test TRUE who actually are TRUE (test positive and actually have cancer)  is pretty small based on two facts provided: the actual percentage of women from the sample who are TRUE (regardless of testing) is only 1%, and the test has a false positive for 9.6% of those tested.

So, my reasoning to solve this is:

1. Assume we have a sample of 1000 women (I use 1000 to reduce the amount I have to talk about fractional people, but I don’t use 10,000 as I get lost in the zeros).

2. We know that the reality is that of the 1,000 women, 10 will have cancer (1%).

990 = no cancer
10 = cancer

3. Of the 10 who have cancer, 8 will test positive
8 out 1000 women tested will test True and are True

4. Of the 990 with no cancer 9.6% will also test positive = 990 * .096 = 95.04. 
95.04 women out of 1,000 will test True but are False.

5. The total number testing true is 8 + 95.04 = 103.04. 
Of these, 8 actually have Cancer.

6. So the value for tests positive (103.04) versus is positive (8) is 8:103.4 or 0.773  or 7.8% 
(8 of the 103.4 = 8/103.4)

Not Being Misled

The key to this and many problems like it is to realize that what you are trying to find is the relationship between those who Test positive vs. the reality of those who are positive, which is why you need all three numbers.

Probability is Weird and Cancer Is Scary

True story: I have a friend who is the head of breast cancer surgery at a very respected hospital. I was at a dinner party where he mentioned that a “very small percentage of those who show up with a positive test actually have cancer.”  This caused a lot of confusion, and that is because we (a) don’t deal with probability well and (b) don’t understand policy tradeoffs as a result.

From a policy point of view, it is too expensive (money, time, etc.) to test everyone with a biopsy, etc.  The preliminary screening is sufficient to move your knowledge from the general 1% probability to the more specific 7.8%, which is enough to pursue. The fact that 20% of women with cancer will get a false negative doesn’t mean that it is better to test everyone, and the fact that out of every 1,000 tested 95 will falsely test positive (and be subjected to needless worry) also does not mean that it isn’t worth using the screening test.

That said, it would be good if, before you took the test, you were told that a positive result means you have less than an 8% chance of actually being positive and that in any case, the test doesn’t change the reality of whether or not you are positive!

This kind of confusion leads to people not flying to countries where there has been a terrorist incident but happily driving across country.

It is estimated that after the 9/11 attack, more people lost their lives by choosing to drive when they otherwise would have flown than were killed in the actual attack.

===

[Migrating the best of Quick Bits to my blog – items of interest to Geeks, tagged Quick Bits]

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

Review: Microsoft Web Cam (QB)

My boss sent me a Microsoft High Def Web Cam, (we do a lot of teleconferencing).  CameraDimensions

I admit to being somewhat skeptical initially; I had a web camera, seemed to work fine.  This new camera blew me out of my seat. 

The overall quality was awesome, and the depth of field extraordinaire (so much so that I had re-tilt the camera so as not to show my messy office!)

The built-in digital microphone appears to be good enough that I can use it with my dictation software and eschew my regular headset, which is very nice. 

I'm particularly fond of the WebCam base. It is made of extremely flexible material, and allows me to attach the camera both to my flatscreen monitor and to my laptop with zero difficulty. WebCamBase

 

This really is the best webcam I've seen.

 

 

 

A quick note on Quick Bits:LifeCamHD

I tried the experiment of presenting “Quick Bits” (too large for twitter, too small for the blog) on separate pages. After reviewing usage and listening to reader feedback, I’ve decided to reintegrate Quick Bits back into the main Blog, but with the Quick Bits tag so that you can read (or avoid) them as they appear.  I’ll migrate the existing ones that are worth keeping over the next week or so.

 

 

 

 

Next