Saturday, 21 June 2008

Silverlight 2.0 Beta 2 - Update on creating a user control to be used as a base class

It appears I was wrong in my previous post.

The ContentPropertyAttribute is seemingly ignored! Although it compiles, and can be edited in Blend, you get a XamlParseException when you try to run it:

"ShellBase does not support Grid as content"

The solution is to manually specify the content property in your Xaml (which kind of sucks!). Strange as this all used to work in Beta 1 (although there were other issues involving namespace parsing which have now been fixed). The correct Xaml looks like this:

<Silverstone:ShellBase x:Class="Silversocial.Client.Views.Shell"

   xmlns="http://schemas.microsoft.com/client/2007"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   xmlns:Silverstone="clr-namespace:Silverstone;assembly=Silverstone">

    <Silverstone:ShellBase.Content>

        <Grid Background="#FF000000">

 

        </Grid>

    </Silverstone:ShellBase.Content>

</Silverstone:ShellBase>

I think (hope) this may be a bug but I will try to confirm this and update my blog if I find out any more.

Thursday, 19 June 2008

MessageBox.Show in Silverlight

Well, it's kind of obvious to anyone that has done any browser based Javascript development, but thought this was worth posting for those of you that do a lot of Windows Development and are wondering how to show a MessageBox in Silverlight. For those of you that have tried it:

MessageBox.Show("Hello world!");

OOPS! Compiler error... that doesn't work, Silverlight does not have a MessageBox class like WPF / WinForms / Win32. Instead, you can ask the browser to do this for you. How do you do this you might ask?

HtmlWindow.Alert("Hello world!");

This is the exact equivalent of the javascript alert function, which would look something like this:

alert("Hello world!");

So what if you want more control? Well, you will find the browser does not provide quite as much as desktop apps for this. In WPF, you can control the message, the title, the icon, the colours, buttons and more! Alert() will simply display a normal dialog with an OK button and the message, and a browser dependent title. The other option the browser gives you is to display a message box with "OK" and "Cancel" options and to be able to read the result of what the user chooses. In WPF you would do:

DialogResult result = MessageBox.Show("Shall I proceed?", "Title", MessageBoxButtons.OKCancel);
if (result == MessageBoxResult.OK)
{
  // process the OK
}

In Silverlight, the equivalent is:

bool result = HtmlPage.Window.Confirm("Shall I proceed?");
if (result)
{
  // process the OK
}

Tuesday, 17 June 2008

Silverlight 2.0 Beta 2 - Creating a user control to be used as a base class

Little tip here that I worked out whilst trying to get the ShellBase class from Silverstone working in the XAML editor and Expression blend.

I started off with an abstract class which derived from UserControl, like follows:

    public abstract class ShellBase : UserControl, IShell

    {

        //... some code

    }

Unfortunately, neither Expression Blend nor Visual Studio's Cider designer likes this, they need to create an instance of the base class for some reason. You get an error message saying it couldn't create an instance of it. Removing the abstract constraint solves this (though violates OO, but I don't mind in this instance!).

Now I have some XAML which looks like this for defining my actual Shell:

<Silverstone:ShellBase x:Class="Silversocial.Client.Views.Shell"

   xmlns="http://schemas.microsoft.com/client/2007"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   xmlns:Silverstone="clr-namespace:Silverstone;assembly=Silverstone">

    <Grid x:Name="LayoutRoot" Background="Black">

 

    </Grid>

</Silverstone:ShellBase>

VS and blend now complain because ShellBase does allow content to be added. The solution I discovered was fairly simple and was twofold:

1. Add a property called Content which shadows the UserControl's Content property and redirects to it

2. Add a ContentProperty attribute to the ShellBase which points to the Content property

    [ContentProperty("Content")]

    public class ShellBase : UserControl, IShell

    {

        /// <summary>

        ///    Redefine the base class's content property to keep the designers happy

        /// </summary>

        public new UIElement Content

        {

            get { return base.Content; }

            set { base.Content = value; }

        }

And that's it! Designer support enabled!

Hope this helps...

Neil

Update - 20/06/2008

It appears I was wrong about the ContentProperty attribute, it doesn't work. See this post for more info

Sunday, 15 June 2008

Silverstone updated for SL 2.0 Beta 2

Just a quick note to inform that the Silverstone project has been updated for Silverlight 2.0 Beta 2. Get it here