I've been working on the middleware and the UI for Wally McClure's Azure "TimedTweet" application that sends scheduled twitter tweets, along
with David Silverlight (no, they didn't name it for him) and a couple of other developers. It's given me
a chance to really get into some of the new features that Silverlight 3 offers.
In Silverlight 3, if you choose the new Silverlight Navigation Application, you get
navigation functionality "out of the box". What I have done here is
to add an additional page, "Profile" that has a DataForm which is bound
to a UserSettings Class that enables users to enter their Username, Password,
Email and Phone and have them stored in Isolated Storage so that these settings
will be read automatically each time the application is loaded.
Silverlight 3 introduces a set of new controls that are specifically aimed at making
the creation of data-centric RIAs easier. These include DataGrid, DataForm, DataPager,
FieldLabel, DescriptionViewer, ErrorSummary, and ChildWindow. The RIA Services May 2009 Preview is out.
I started out with a UserSettings class:
Because of the System.ComponentModel.DataAnnotations namespace, we are able to apply
Validation Attributes directly on the public fields of our class, and even include
appropriate error messages. The Silverlight controls such as DataForm pick these
up automatically; they even take care of displaying the validation errors. The
developer doesn't have to write a single line of code (although you can if you
want to). In this case I've made all the fields [Required] and for the Email,
I've provided a [RegularExpression] validator as well. Note also that each field
carries the [Bindable(true, BindingDirection
.TwoWay)] attribute. This will enable my Dataform to handle not only automatic display
of each field name and value, but also for it to be enabled to edit and save
the form data. I don't have to create any form fields at all. That is a whole
bunch of code that I never enjoyed writing, and now I don't have to do it anymore!
Let's switch over to the App.xaml.cs class and see how that got wired up:
The logic mostly just attempts to load the UserSettings from IsolatedStorage, using
the DataContractSerializer to give us an instance of the UserSettings class,
and if it isn't there or the data is not valid, it sets the boolean "NeedsProfileData"
field. This is checked later in the application to direct the user to the Profile
Page:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (App.NeedsProfileData)
{
this.Frame.Navigate(new Uri("/Views/Profile.xaml", UriKind.Relative));
return;
}
}
void
Now let's have a look at how the DataForm on Profile.xaml works. Here is the Xaml.
As you can see, it is quite simple:
<navigation:Page xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm" x:Class="SLUserSettings.Views.Profile"
xmlns=""
xmlns:x=""
xmlns:
<Grid x:
<StackPanel>
<TextBlock Text="Profile" Style="{StaticResource HeaderTextStyle}"/>
<StackPanel Style="{StaticResource ContentTextPanelStyle}" Orientation="Vertical">
<TextBlock Text="Configure User Settings" Style="{StaticResource ContentTextStyle}"/>
<my:DataForm
</my:DataForm>
</StackPanel>
</StackPanel>
</Grid>
</navigation:Page>
And here is all that is needed in the codebehind:
<
You can download the Silverlight 3 Visual Studio Solution here.