I started playing around with Siverlight earlier this week. From what I've seen so far, it looks like a very nice environment - it has (most of) the power of WPF for building user interfaces, and (most of) the power of the .NET Base Class Library to implement whatever it is that your application needs to do.
There are a few gotchas, though - especially if you have any experience with WPF. There are some subtle features that are missing, that you don't realize how nice they are until they're gone (such as binding to a static property, for example).
I encountered one such incident of this when trying to get data binding working. I first wrote my Model class, which looked something like this:
namespace SilverlightTestApp
{
class Model : INotifyPropertyChanged
{
public Model()
{
}
private string firstName = string.Empty;
public string FirstName
{
get { return this.firstName; }
set
{
this.firstName = value;
this.OnPropertyChanged("FirstName");
}
}
private string lastName = string.Empty;
public string LastName
{
get { return this.lastName; }
set
{
this.lastName = value;
this.OnPropertyChanged("LastName");
}
}
/* INotifyPropertyChanged Implementation */
}
}
So far, so good. I then wrote some XAML to bind display this information in the UI:
<UserControl>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">First Name:</TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}" />
<TextBlock Grid.Row="1" Grid.Column="0">Last Name:</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}" />
<Button Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right">
<TextBlock>Click Me</TextBlock>
</Button>
</Grid>
</UserControl>
In the code behind, an instance of the Model class is set as the DataContext of the UserControl, and in the click handler for the button a MessageBox is display with the name entered.
Looks good, right? Except when I run the program I get a MethodAccessException. No matter what I did, I could not figure out how to fix this error. The only thing I did know is that it had something do with my bindings, since if I took those out the error would go away. A search on the Internet didn't help much, either.
Then, on a hunch, I changed the Model class to be public instead of private, and that fixed it! Apparently in Silverlight binding only works with public classes (which isn't the case in WPF). My guess is that this is due to security - since Silverlight only runs in the browser (at least today) in a partial trust environment, Reflection (which is how binding retrieves the values it needs) on non-public members must not be allowed.
While this was frustrating, it was a good learning experience. It seems like I need to keep security in mind more with Silverlight development than with WPF.
Recent Comments