Click here to Skip to main content
Click here to Skip to main content
Go to top

Flexibox – A Silverlight alternative to Lightbox

, 10 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Flexibox is an alternative to Lightbox, displaying multiple resolutions of an image without needing a popup overlay. Flexibox shows how a Silverlight app can resize itself with a page.

Introduction

Great photos need to displayed big to be best appreciated, but smaller image sizes work best within a block of text or as a collection of thumbnails. To solve this design dilemma, thumbnail images often contain a link to view a larger version of the same image. If this takes the user to another page, then this is inconvenient as the user has to go back to the original page to continue.

A popular alternative is to use a JavaScript library, such as Lightbox, to display an overlay image on the current page. This is very impressive the first few times one uses it. However, the overlay hides the rest of the page. The distraction and time taken acts as a disincentive for users to see the larger version of the image.

Flexibox displays a single image within a HTML page in the same way as an <img> tag. Flexibox can change the image it displays to one of a different size, and automatically resizes itself within the page. This enables a user to go from a thumbnail to a large resolution image without needing to leave the page.

The Flexibox appears on a web page just like any thumbnail image, but has an enlarge button overlaid on the top right to indicate to the user there is a larger version.

When the user clicks on the enlarge button, the Flexibox is enlarged instantly, causing the surrounding page elements to be re-formatted to flow around the now larger control.

To see a Flexibox live, go to http://ithinkly.com/demo/flexibox/.

The Code

The code demonstrates a number of useful Silverlight techniques:

  • Passing parameters from the HTML page to Silverlight
  • Converting CSS color strings into Silverlight Color structures
  • Loading content relative to the host page
  • Asynchronous loading and caching of images
  • Resizing a Silverlight control within a page

For what seems to be a very simple application, Flexibox contains a lot more code than can be covered here, so only the techniques listed above are described. The full source code is provided, so you can study all the details in full. The project was developed with VS 2010, targeting Silverlight 3.

The HTML

The first thing to study is the way the Flexibox control HTML differs from the standard.

<div style="margin-right: 8px; margin-bottom: 3px;float: left;">
 <object data="data:application/x-silverlight-2," 
        type="application/x-silverlight-2" 
        width="240px" height="161px">
  <param name="source" value="ClientBin/FlexiBox.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="3.0.40818.0" />
  <param name="autoUpgrade" value="true" />
  <param name="initParams" 
    value="border=2,border_color=#444,preload=true,mode=small,
            thumb_url=images/s1_100.jpg,medium_url=images/s1_500.jpg,
            large_url=images/s1_1024.jpg"  />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" 
                   style="text-decoration:none">
    <img src="http://go.microsoft.com/fwlink/?LinkId=161376" 
         alt="Get Microsoft Silverlight" style="border-style:none"/>
  </a>
</object><iframe id="_sl_historyFrame" 
  style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

The object tag is given a fixed width and height in pixels. These attributes will be later changed from within Silverlight. The div tag surrounding the object tag uses an in-line style to set the margin and float the control to the left of the surrounding text. You can use almost any style in here, except for width or height. Also, do not set an ID or class style for the div tag, as the default Visual Studio generated page will do.

The initParams param tag contains the parameters to be passed to the Silverlight control. The example above sets the width and color of the border, and passes in four images. The image URLs are relative to the page, just as one would use in an img tag. The mode is set to small so that the small image will be the first one displayed.

The Flexibox XAML

There is only one control in the application, and that is defined as follows:

<Border BorderBrush="{Binding Path=BorderBrush}" 
        BorderThickness="{Binding Path=BorderThickness}" Name="border1"  >
    <Grid x:Name="LayoutRoot" >
        <Image  Name="image1" Stretch="None" Source="{Binding Path=DisplayedImage}" />
        <controlsToolkit:BusyIndicator Height="59" 
             HorizontalAlignment="Center"  Name="busyIndicator1" 
             VerticalAlignment="Center" Width="151" 
             IsBusy="{Binding Path=IsBusy}" BusyContent="{Binding Path=BusyStatus}" 
             DisplayAfter="00:00:02.1000000" IsEnabled="True" />
        <StackPanel Height="25" HorizontalAlignment="Right" 
                Margin="0,2,2,0" Name="stackPanelButtons" 
                VerticalAlignment="Top"  Orientation="Horizontal">
            <Button Style="{StaticResource ButtonIcon}" 
                Click="Contract_Button_Click" Name="ContractButton" 
                RenderTransform="{StaticResource ButtonBottomLeft}" 
                Visibility="{Binding Path=ContractVisible}" />
            <Button Style="{StaticResource ButtonIcon}" 
               Click="Expand_Button_Click" Name="ExpandButton" 
               RenderTransform="{StaticResource ButtonTopRight}" 
               Visibility="{Binding Path=ExpandVisible}" />
        </StackPanel>
    </Grid>
</Border>

As you would expect, there is a Border, an Image control, and a busy indicator for when loading the first image. The StackPanel is used to align the two buttons, which only become visible when they can be used. The buttons are simply re-styled. See the styles.xaml file for details. All the dynamic elements are controlled by data binding to properties of the model.

The Code-Behind File

public partial class MainPage : UserControl
{
    FlexiBoxViewModel viewModel = new FlexiBoxViewModel();

    public MainPage()
    {
        InitializeComponent();
        DataContext = viewModel;
    }
    private void Contract_Button_Click(object sender, RoutedEventArgs e)
    {
        viewModel.Contract();
    }
    private void Expand_Button_Click(object sender, RoutedEventArgs e)
    {
        viewModel.Expand();
    }
}

As can be seen, all the interesting stuff is in the viewModel. Handlers for the button clicks are required as we are using Silverlight 3. In 4, we can get rid of these and use commands directly within the XAML.

The App.xaml.cs file contains only the generated code, so no need to look at that.

Reading the Parameters from the HTML Page

foreach (string key in Application.Current.Host.InitParams.Keys)
    ParseExternalParam(key, Application.Current.Host.InitParams[key]);

The Application Host contains a dictionary of all the parameters passed into the control, which can be processed one by one.

private void ParseExternalParam(string key, string value)
{
    try
    {
    ...
        else if (String.Compare("thumb_url", key, 
                 StringComparison.InvariantCultureIgnoreCase) == 0)
        {
            images[(int)FlexiImageModel.Size.thumb].Url = value;
        }
        else if (String.Compare("border", key, 
                 StringComparison.InvariantCultureIgnoreCase) == 0)
        {
            borderSize = Double.Parse(value);
        }
        else if (String.Compare("border_color", key, 
                 StringComparison.InvariantCultureIgnoreCase) == 0)
        {
            borderColor = value.ToColor();
        }
    }
    catch
    {
        // contain any exceptions thrown by invalid params
    }
}

Each parameter key is checked for a match, and then the value is converted into the appropriate type and stored for later use. As elsewhere in the code, exceptions are caught and safely ignored. If this is not done, then entering an invalid number for the border parameter would cause a nasty error message to be displayed to the user. This may be what you want, in which case, remove the try-catches, but I prefer the app to fail quietly and continue in the same way XAML does when it finds errors.

Unlike the full version of .NET, there is no built-in way of converting a string representation of a color into a color object in Silverlight. Many people have had to tackle this in the last two years, and there are plenty of examples on the web, but most just do hex strings in one format. I wanted a color string converter that could handle all the CSS color formats. E.g.: '#FFFFFF', '#FFF', 'white'.

So I wrote the following utility class:

public static class TWSilverlightUtilities
{
    // Converts a CSS style string into Color structure.
    // Returns black if the input is invalid.
    // The color string to convert. May be in one
    // of the following formats; #FFFFFFFF, #FFFFFF, #FFF, white.
    public static Color ToColor(this string str)
    {
        Color rv = Color.FromArgb(0xFF,0,0,0);

        try
        {
            rv = str.ToColorEx();
        }
        catch
        {
        }

        return rv;
    }

    public static Color ToColorEx(this string str)
    {
        // empty string check
        if ((str == null) || (str.Length == 0))
            throw new Exception("empty color string");

        // This is the only way to access the colors that XAML knows about!
        String xamlString = "<Canvas xmlns=\"http://schemas." + 
                            "microsoft.com/winfx/2006/xaml/" + 
                            "presentation\" Background=\"" + 
                            str + "\"/>";
        Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
        SolidColorBrush brush = (SolidColorBrush)c.Background;

        return brush.Color;
    }
}

I really wanted to write this as an extension method of Color, as it would then match the ToString method nicely. Unfortunately, Color is a struct, and extension methods do not work properly on them as the 'this' parameter can only be passed by value and not by reference, which makes it useless for structs. Also, you cannot define a static extension method, only instantiate one. Both serious omissions in the language, in my view.

Loading Content Relative to the Host Page

The URLs of the images to display could be given as absolute or relative to the web page. The Silverlight control may be hosted in a completely different location, so the correct address must be used, which is obtained from HtmlPage.Document.DocumentUri. It's then a matter of stripping off the page name, query, and fragment, and appending the relative image URL, to give us the absolute URL of the image that is required.

private Uri GetImageUri(FlexiImageModel.Size size)
{
    Uri imgUri = new Uri(images[(int)size].Url, 
                     UriKind.RelativeOrAbsolute);

    if (imgUri.IsAbsoluteUri)
        return imgUri;

    // Make an absolute URL relative to the document we are in
    UriBuilder pageUri = new UriBuilder(HtmlPage.Document.DocumentUri);
    pageUri.Fragment = null;
    pageUri.Query = null;
    int n = pageUri.Path.LastIndexOf('/');
    if (n > 0)
        pageUri.Path = pageUri.Path.Substring(0, n + 1);

    return new Uri(pageUri.Uri, imgUri);
}

Asynchronous Loading and Caching of Images

Each Flexibox can hold up to four images: thumb, small, medium, and large. These can either be loaded one at a time on request, or all pre-loaded. The pre-load feature gives the user instant access to the different sized images, but uses more bandwidth, so each option has its uses. When pre-loading, the most important thing is to load the image to be initially shown first. Only when that is downloaded should the other images be downloaded and cached. It is possible that the user may click on the expand button while the next image is being downloaded, and the code needs to cope with that. The caching of the images is plain C#, and not a Silverlight technique, so it is not explained in detail here, but is in the attached source code.

private void RetrieveImage(FlexiImageModel.Size size)
{
    try
    {
        // Dont start a second retrieve of the same image
        if (!images[(int)size].IsRetrieving)
        {
            Uri uri = GetImageUri(size);
            images[(int)size].IsRetrieving = true;
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += 
              new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(uri, size);
        }
    }
    catch (Exception ex)
    {
        images[(int)size].IsError = true;
    }
}

To download an image, firstly get its full URL as described above, and then use a WebClient to start the download asynchronously so as not to block the user interface. When starting the operation, we pass in the size of the image, so we know which image has been downloaded when it completes.

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    FlexiImageModel.Size size = newSizeMode;
    if (e.UserState is FlexiImageModel.Size)
        size = (FlexiImageModel.Size)e.UserState;

    try
    {
        if ((e.Cancelled) || (e.Error != null))
            throw new Exception("Error downloading image");

        BitmapImage bitmap = new BitmapImage();
        bitmap.SetSource(e.Result);
        e.Result.Close();
        images[(int)size].Bitmap = bitmap;
        images[(int)size].IsError = false;
    }
    catch
    {
        images[(int)size].IsError = true;
        BusyStatus = "Error loading image";
    }

    images[(int)size].IsRetrieving = false;

    LoadImage();

    RetrieveNextImage();
}

WebClient has several options on how to receive the downloaded data. By using OpenReadAsync, it provides an open stream in e.Result, which is perfect to set as the source for the new BitmapImage object. Of course, things can go wrong, and any errors are caught and the image marked as an error in the cache.

Once the image has been cached, Flexibox is ready to load it into the user control and then start the download of the next image.

Loading an Image into the User Interface

private void LoadImage()
{
    if ( ((imageLoaded) && (sizeMode == newSizeMode)) ||
        (images[(int)newSizeMode].IsError) || (!images[(int)newSizeMode].IsLoaded))
        return; // nothing to do

    sizeMode = newSizeMode;
    imageLoaded = true;

    ContractVisible = (sizeMode != NextSmallerImage(sizeMode)) ? 
                       Visibility.Visible : Visibility.Collapsed;
    ExpandVisible = (sizeMode != NextBiggerImage(sizeMode)) ? 
                     Visibility.Visible : Visibility.Collapsed;

    ResizeControl();

    NotifyPropertyChanged("DisplayedImage");
    IsBusy = false;
}

The method first checks for errors and if the required image is already loaded, before proceeding with the rest of the operation.

The Expand and Contract buttons need to be turned on or off depending on whether there is a bigger or smaller image available.

public Visibility ExpandVisible { get { return expandVisible; } 
       set { expandVisible = value; NotifyPropertyChanged("ExpandVisible"); } }
public Visibility ContractVisible { get { return contractVisible; } 
       set { contractVisible = value; NotifyPropertyChanged("ContractVisible"); } }

This is done by setting the two public properties that the XAML defined buttons are bound to.

public BitmapImage DisplayedImage { get { return images[(int)sizeMode].Bitmap; } }

The XAML defined image is bound to the DisplayedImage property, which retrieves the image directly from the cache. However, the image will not be updated as it needs to be told that the selected image has changed. This is done with the NotifyPropertyChanged call at the exact time required, which is after the control has first been resized to fit the new image.

Resizing a Silverlight Control within a Page

Now for the final technique, and the one that this control is really all about: resizing a Silverlight control within a page. I want to give full credit for this technique to Charles Petzold.

private void ResizeControl()
{
    if (!images[(int)sizeMode].IsLoaded)
        return;

    double height = Math.Max(20,(2 * borderSize) + 
                    images[(int)sizeMode].Bitmap.PixelHeight);
    double width = Math.Max(20,(2 * borderSize) + 
                   images[(int)sizeMode].Bitmap.PixelWidth);

    if (controllWidth != width)
    {
        controllWidth = width;
        SetDimensionPixelValue("width", controllWidth);
    }

    if (controllHeight != height)
    {
        controllHeight = height;
        SetDimensionPixelValue("height", controllHeight);
    }
}

Each bitmap image exposes its dimensions, so we can calculate the new width and height of the control by adding these to the border thickness. It is then a matter of setting the width and height using this method.

void SetDimensionPixelValue(string style, double value)
{
    HtmlPage.Plugin.SetAttribute(style, 
           ((int)Math.Round(value)).ToString() + "px");
}

HtmlPage.Plugin provides access to the HtmlElement of the object tag. It is then just a case of setting either the width or the height attribute to a text definition of the measurement. In order for this to work, it is important that the page does not contain additional width and height settings that may affect the control within the page or one of its div containers. This is done, by default, using styles in the generated code.

Conclusion

As well as explaining a number of basic Silverlight techniques, I am hoping that the control itself shows how Silverlight can be used as an integral part of a web page design.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Tom F Wright
Software Developer
United Kingdom United Kingdom
Tom has been developing software for 20 years and has done a lot of different stuff. Recently this has mostly been C# WPF/Silverlight, ASP.Net and Ruby On Rails.
 
Blog: http://rightondevelopment.blogspot.com/

Comments and Discussions

 
GeneralBusyIndicator does not exist Pinmemberyalenap13-Apr-10 8:51 
GeneralRe: BusyIndicator does not exist PinmemberTom F Wright13-Apr-10 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 10 Mar 2010
Article Copyright 2010 by Tom F Wright
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid