Learn Blend in a Month (22 posts) from Silverlight Buzz

Silverlight SDK sample browser added Source Viewer

The Silverlight SDK sample browser lets you explore all the live samples used by the official Silverlight documentation.

In the past, the source code for the samples could only be found in the documentation. Now, however, you can view the source directly from the sample browser by clicking a new button in the header section.

sampleHeader

When you click the button, a new browser window opens and displays the code.

sourceViewer

Training courseware for Telerik RadControls for Silverlight

image

This training material will help you quickly get up to speed with Silverlight and will discuss some of the issues in common with all RadControls such as binding or theming, working with RIA Services (also WCF and ADO.NET), working with Expression Blend, etc.  

The courseware is available to everyone for free download from our website:
PDF (47MB)

The sample C# projects can now be downloaded along with the book at: http://www.telerik.com/support/documentation-and-tutorials/step-by-step-tutorial-for-silverlight.aspx.

Simple Dialog Service in Silverlight – It is an Asynchronous Process!

I noticed on the forums there are a lot of users not comfortable with asynchronous programming who struggle a bit in Silverlight with getting their arms around the concept of a dialog box. In other environments, you can simply shoot out a dialog, wait for the response, and continue. In Silverlight, of course, the action is asynchronous. I would argue it should be this way most of the time.

The problem is that many people tend to take the approach of trying to force the process into a synchronous one, instead of changing the way they think and approach the application. There is a reason why processes are asynchronous in Silverlight. There is one main UI thread, and a blocking process would block the entire thread and effectively freeze the Silverlight application. Having the process asynchronous allows you to continue to render graphics elements, perform actions in the background, even display a soothing animation while you await the user’s response.

Note: the use of Action<bool> Response for callback & dialog.Show() is not a blocking call. This allow the Silverlight UI Thread to continue and Wait for the Response from the ChildWindow Dialog

Dialog is derived from ChildWindow
public class DialogService : IDialogService
{
    public void ShowDialog(string title, string message, bool allowCancel, Action<bool> response)
    {
        var dialog = new Dialog(allowCancel) {Title = title, Message = message, CloseAction = response};
        dialog.Closed += DialogClosed;
        dialog.Show();
    }

    static void DialogClosed(object sender, EventArgs e)
    {
        var dialog = sender as Dialog;
        if (dialog != null)
        {
            dialog.Closed -= DialogClosed; 
            dialog.CloseAction(dialog.DialogResult == true);
        }
    }
}
// Using the Dialog
public partial class MainPage : UserControl
{
    private bool _toggleState = true;
    private readonly IDialogService _dialog = new DialogService();

    public MainPage()
    {
        InitializeComponent();
    }
// Example 1
    private void ToggleEnabled_Click(object sender, RoutedEventArgs e)
    {
        _dialog.ShowDialog(
            _toggleState ? "Disable Button" : "Enable Button",
            _toggleState
                ? "Are you sure you want to disable the dialog button?"
                : "Are you sure you want to enable the dialog button?",
            true,
            r =>
                {
                    if (r)
                    {
                        _toggleState = !_toggleState;
                        DialogButton.IsEnabled = _toggleState;
                        ToggleEnabled.Content = _toggleState ? "Disable Dialog Button" : "Enable Dialog Button";
                    }
                });
    }
// Example 2
    private void DialogButton_Click(object sender, RoutedEventArgs e)
    {
        _dialog.ShowDialog(
            "Confirm This",
            "You have to either close the window or click OK to confirm this.",
            false,
            r => { TextDialog.Text = r ? "You clicked OK." : "You closed the dialog."; });
    }
}
For more goto: http://csharperimage.jeremylikness.com/2010/01/simple-dialog-service-in-silverlight.html

ICommand in Silverlight 4

In Silverlight 4, ButtonBase (and all its derived versions) and HyperlinkButton now support Command and CommandParameter to tie ICommand implementations.  For example, in XAML I can tie two different buttons to the SaveCommand that I have on some object i’ve bound to my UI:

<Border BorderBrush="LightGray"
        BorderThickness="1">
  <StackPanel Orientation="Horizontal">
    <TextBlock>Tools:</TextBlock>
    <Button Command="{Binding SaveCommand}"
            CommandParameter="{Binding Person}"
            Content="Save" />
  </StackPanel>
</Border>
<StackPanel Grid.Row="1">
  <TextBlock FontWeight="Bold"
             Text="Person" />
  <TextBlock Text="Name" />
  <TextBox Text="{Binding Person.Name, Mode=TwoWay}" />
  <TextBlock Text="Occupation" />
  <TextBox Text="{Binding Person.Occupation, Mode=TwoWay}" />
  <Button Command="{Binding SaveCommand}"
          CommandParameter="{Binding Person}"
          Width="100"
          HorizontalAlignment="Right"
          Content="Save" />
</StackPanel>

By binding a SaveCommand to both of these buttons, whenever either of the buttons is pressed, the command will be executed.  But perhaps even more important is that as the command isn’t valid (perhaps when the Person doesn’t have changes), the buttons will be disabled.

For more on how to implement the command in C# goto http://wildermuth.com/2010/01/21/New_SL4_Feature_Commanding