The following code shows a XAML example (page.xaml) that is included in the .xap application package referenced in the earlier code example.
XAML
<UserControl x:Class="myExample.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Button Demonstration" Margin="0,20,10,15"
FontFamily="Verdana" FontSize="18" FontWeight="Bold"
Foreground="#FF5C9AC9" Grid.Row="0" Grid.ColumnSpan="2"/>
<Button x:Name="btn1" Grid.Row="1" Margin ="0,5,5,5"
HorizontalAlignment="Left"
Foreground="Green" Width="120" Click="OnClick1"
Content="Hover to Click" ClickMode="Hover" />
<TextBlock x:Name="text1" Grid.Row="1" Grid.Column="1"
Margin ="0,8,0,0" />
<Button x:Name="btn2" Grid.Row="2" Margin ="0,5,5,5"
HorizontalAlignment="Left"
Foreground="Blue" Width="120" Click="OnClick2"
Content="Press to Click" ClickMode="Press" />
<TextBlock x:Name="text2" Grid.Row="2" Grid.Column="1"
Margin="0,8,0,0" />
<Button x:Name="btn3" Grid.Row="3" Margin ="0,5,5,5"
HorizontalAlignment="Left"
Click="OnClick3" Width="120" Content="Reset"
ClickMode="Release"/>
<TextBlock x:Name="text3" Grid.Row="3" Grid.Column="1"
Margin ="0,8,0,0" />
</Grid>
</UserControl>
The following code shows the supporting code-behind (page.xaml.cs or page.xaml.vb) for the previous XAML example (page.xaml).
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub OnClick1(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Foreground = New SolidColorBrush(Colors.Blue)
text1.Text = "Click event handled on Hover."
text3.Text = ""
End Sub
Private Sub OnClick2(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn2.Foreground = New SolidColorBrush(Colors.Green)
text2.Text = "Click event handled on Press."
text3.Text = ""
End Sub
Private Sub OnClick3(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Foreground = New SolidColorBrush(Colors.Green)
btn2.Foreground = New SolidColorBrush(Colors.Blue)
text1.Text = ""
text2.Text = ""
text3.Text = "Click event handled on Release."
End Sub
End Class
Using JavaScript and XAML with the Silverlight Server Control
The following example shows the markup for a Silverlight server control that defines a simple calculator in XAML. The logic for interacting with the XAML is defined in a separate JavaScript class. The Silverlight server control references the file that contains the XAML markup (Calculator.xaml). You can also see how to reference the client-script library (Calculator.js) for the calculator code, and how to reference the JavaScript class (Custom.Calculator).
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Name="SilverlightControl.js"
Assembly="System.Web.Silverlight"/>
<asp:ScriptReference Path="calculator.js" />
</Scripts>
</asp:ScriptManager>
<asp:Silverlight runat="server" ID="Silverlight1"
Height="340"
Width="320"
Source="../calculator.xaml"
ScriptType="Custom.Calculator"
OnPluginError="onXamlError">
In its basic form, the Silverlight server control provides a JavaScript type that includes members such as the onPluginLoaded method and the onPluginError method. The Silverlight server control references a XAML file through the Source property. The code for interacting with the XAML can be in a managed-code assembly or in a client JavaScript library (.js file).