Skip to main content
/_layouts/images/titlegraphic.gif

Peter Himschoot's Windows Foundation Trilogy

Go Search
Peter Himschoot's Windows Foundation Trilogy
U2U Website
  

Talking about .NET, Windows Presentation Foundation, Workflow Foundation and Communication Foundation

 Image Web Part

Peter at U2U booth during TechEd Eur 2006
  U2U Home
  U2U Course Calendar
Talking about .NET, Windows Presentation Foundation, Workflow Foundation and Communication Foundation
Debugging with source
I must say, one of the things I really like in Visual Studio 2008 now is the debugging with sources feature, allowing you to see the source of the .NET framework. Currently most of the base framework is available, and so is WinForms/WPF/... More to come.
 
I really like this because now I can learn more about the inner working of the framework and why my code is behaving in a certain fashion. Before you could also do this, using Reflector, but seeing the orginal sources with full local variable names is a lot more practical.
 
I do confess, when I am on the road the feature does require an active internet connection (because it checks to see if a newer version of the source is available, I think) and I do think it should work without a connection. Next version please :)
 
And it is soo easy to setup.
 
 
DevDays NL
Yesterday I gave two sessions @ DevDays NL:
 
On was a Silverlight End to End application, about belgian beers ;)
 
The other was on using REST and JSON with WCF
 
Both can be found here
(oeps, looks like the link dropped off)
 
MSDN ChopStick Noodles

I am happy to announce that I've started a series of screencasts called ChopStick Noodles (you eat noodles with chopsticks, don't you? J).

The first two are about the background worker (for which I've previously written an article) and about creating your own code snippets.

There is also a third one about improvements in Visual Studio 2008 for using datasets in enterprise development, but that one hasn't been published.

If you have suggestions for future screencasts, please let me know (why not add a comment J).

I'm thinking about one on Isolated Storage, one on XAML, and a series on Silverlight.

Hilarious movie about Silverlight!!!

Silverlight Rehab! Watch it here!!!

Silverlight 2.0 DataBinding Explained (a bit) :)

 

Silverlight supports data binding, but currently there is a lack of samples explaining some of the nicer concepts, plus some of the things are not as you might expect.

Data binding in Silverlight is through the {Binding <Path>} syntax, for example to bind a TextBox's Text property to the Value property of some class you would use

<TextBox Text="{Binding Value}"  />

The catch is that this is a one way binding, if you change the Text in the TextBox the Age property doesn't get updated...

The solution is simple, you need to set the Mode property of the Binding object to TwoWay like this:

<TextBox Text="{Binding Value, Mode=TwoWay}"  />

Now modifying the Text property will modify the Value property (and vice versa).

Next on my agenda is data conversion. You can do this by using a class that implement the IValueConverter interface, for example:

public class DoubleToBrushConverter  : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    string v = (value ?? "0").ToString();
    double cutoff = parameter == null ? 30 : double.Parse( parameter.ToString() );

    if (double.Parse(v) < cutoff)
      return new SolidColorBrush(Colors.Green);
    else
      return new SolidColorBrush(Colors.Red);
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

This class will convert the value to a brush, nice if you want to show the user that an invalid or unusual value was entered. You can use this as follows:

<TextBox Text="{Binding Value, Mode=TwoWay}" 
         Background="{Binding Value, 
                              Converter={StaticResource converter},
                              ConverterParameter=40}" />

The ConverterParameter will get passed into the Convert method as the parameter parameter.

Please note that we don't need to implement the ConvertBack method of the IValueConverter interface because the user will not be able to change the background color.

image

When the value goes above 40 the background turns red.

Last thing is the Source property of the Binding element. With this you can point to some object on your Page, stored as a resource:

<TextBox Text="{Binding Value, 
                  Mode=TwoWay, 
                  Source={StaticResource someObject}}"  />

 

This of course opens up using Element binding, which means binding to elements of your page to each other. The technique I propose is using some temporary object and binding the controls to this object.

For example, to bind three controls (two textboxes and one slider) to each other use following markup:

<TextBox Text="{Binding Value, Mode=TwoWay, Source={StaticResource someObject}}"  />
<TextBox Text="{Binding Value, Mode=TwoWay, Source={StaticResource someObject}}" 
         Background="{Binding Value,
                              Source={StaticResource someObject},
                              Converter={StaticResource converter}, 
                              ConverterParameter=40}" />

<Slider x:Name="theSlider"
  Minimum="0" Maximum="100" SmallChange="1" LargeChange="10"  
  Value="{Binding Value, Mode=TwoWay, Source={StaticResource someObject}}" />

Each binding refers to the same Source, which I've declared on top of the page resources:

<UserControl.Resources>
  <u:DoubleToBrushConverter x:Key="converter" />
  <u:ElementBinding x:Key="someObject" Value="66" />
</UserControl.Resources>

The DoubleToBrushConverter class is the one on top, the ElementBinding class looks like this:

public class ElementBinding : INotifyPropertyChanged
{
  double val;

  public double Value
  {
    get { return val; }
    set { val = value; OnPropertyChanged("Value"); }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propName)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propName));
  }
}

The class implements the standard INotifyPropertyChanged interface to support databinding.

A demo project containing this sample can be found here.

Fun with DeepZoom

I've been experimenting a little bit with DeepZoom, a new extension for Silverlight, currently in beta from microsoft. This works together with the MultiScaleImage control from silverlight, giving you a very performant and smooth picture browsing experience.

It does this by dividing a large image into 256x256 squares and building a pyramid of smaller sized images, again divided into 256x256 squares, up until it reaches a single pixel image. This adds 33% of extra space required on disk, which I believe isn't very much.

This way it can load the squares it needs at the right zoom level, reducing the amount of pixels downloaded (at the same time).

To try this yourself, you first need the the Deep Zoom Composer utility:

http://blogs.msdn.com/expression/archive/2008/03/05/download-the-preview-of-the-deep-zoom-composer.aspx

Start it up and create a new project:

DZ_CreateProj

On the Import tab click the "Add Image..." button and import any picture you want:

DZ_Ïmport

Next click on the Compose tab and position your pictures the way you want:

DZ_Compose

And as the last step export your stuff (first choose a name for your export):

DZ_Export

This will create a directory structure:

Heroes

With Visual Studio 2008, create a new Silverlight project with default settings (with extra web project). Build the project, then copy the above contents into the web project's ClientBin folder (you need to build the project before you will see this folder).

Then in page.xaml first remove the Width and Height properties, then add a MultiScaleImage control with source property pointing to the info.bin file:

 

<UserControl x:Class="DeepZoomDemo.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Grid x:Name="LayoutRoot" Background="White">

    <MultiScaleImage Source="info.bin" />

  </Grid>
</UserControl>

Build your project and run. That's it!

Next entry will show how to add Zoom and move support to this solution.

Launch & TechDays Sessions

As promised, my launch and TechDays slides and demos can be found at http://www.u2u.be/res/Peter.aspx.

Simply scroll down to presentations...

Wasting your time: When the Simpsons meet South Park

As a big fan of the Simpsons and South Park I can't resist showing this marriage between the two:

 

WCF Service in Visual Studio "locking up" for a minute

When you build a WCF service using Visual Studio 2008 using the WCF Service Library project template, Visual Studio automatically hosts the service library using the WcfSvcHost.exe application. However, when you're simply hosting this service in another service host (like IIS) you don't need this WcfSvcHost.

To make this even more annoying, if the service configuration is incorrect (why would you want to fix something you're not using anyway???) you will see this warning when starting to debug "The target assembly contains no service types. etc...":

 image

This is actually caused by the WcfSvcHost searching your assembly for service types...

There is an easy way to get rid of this, simply delete the app.config file in the service library. You don't need config files in service libraries anyway. Beware, there might be useful configuration information like connection strings, so copy these over to your host's config before deleting.

I tried simply deleting the service configuration from the config file, but the tool still insists in you configuring the service, so this doesn't help. If you still need a .config file in your service library, avoid the WCF Service Library template, use the normal library project instead and add references to System.ServiceModel and System.Runtime.Serialization.

Or you can try to delete the following line from your project:

<ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

This deletes the WcfSvcHost from starting.

First look at Silverlight 2 - ScottGu Tutorial

Well, I've just completed the full tutorial from Scott Gu, and I must say it was a pleasure to do. I already have a lot of experience with WPF, XAML and Silverlight, so it was a big eye-opener into Silverlight's new features. For those of you who've attended my WPF training I told you Silverlight would have databinding and styles/templating, and I must say it really fulfills it promise. At the end the lab even makes you use your Silverlight controls in an WPF application. Cool!

1 - 10 Next

 ‭(Hidden)‬ Admin Links