Thursday, May 28, 2009

Silverlight 3 Beta and ChildWindow

The Silverlight 3 beta introduces the concept of the ChildWindow as noted in the following blog posts:

http://www.wintellect.com/CS/blogs/jprosise/archive/2009/04/29/silverlight-3-s-new-child-windows.aspx
http://timheuer.com/blog/archive/2009/05/10/silverlight-childwindow-non-modal-refactor.aspx

It does return a "DialogResult", however unlike other modals in the .NET paradigm the "DialogResult" is a nullable bool (bool?) value. What happened to the standard "DialogResult" enumerations?

Hopefully this will be rectified in the RTM release of Silverlight 3, but in the mean time a simple expanded class can be used that returns the Dialog result.



using System;
using System.Windows;

namespace System.Windows.Controls
{
public class ChildWindowEx : ChildWindow
{
public DialogResults DialogResultEx
{
get { return _result; }
protected set
{
_result = value;
SetDialogResult();
}
}

#region Protected Methods
protected virtual void SetDialogResult()
{
switch (_result)
{
case DialogResults.OK:
case DialogResults.Yes:
DialogResult = true;
break;
default:
DialogResult = false;
break;
}
}
#endregion

#region Fields
private DialogResults _result;
#endregion

public enum DialogResults
{
None,
OK,
Cancel,
Abort,
Retry,
Ignore,
Yes,
No
}
}
}



The ChildWindowEx should be put in an utility Silverlight assembly such as "MySilverlightControls" although it can be defined in your Silverlight application's assembly.

The above class can be used to create a control by first creating a new ChildWindow, then changing the parent. The class defintion should look like the following:


namespace Sivlerlight1
{
public partial class ChildWindow1 : ChildWindowEx
{
...
}
}


And the XAML should have the following definition:


xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myControls="clr-namespace:System.Windows.Controls;assembly=MySilverlightControls">



It is important to note that both the parent of the class and the XAML use the ChildWindowsEx otherwise the ChildWindow1.g.cs (or .vb) class that gets generated will have compile errors.

In the XAML example above, we have delcared a new namespace via the 'xmlns:myControls="clr-namespace:System.Windows.Controls;assembly=MySilverlightControls"' declaration. This declaration allows us to use any controls (or other UI elements) that are included in the System.Windows.Controls namespace and declared in the "MySilverlightControls" assembly.

0 comments:

Post a Comment