Embedding Multiple Silverlight Custom Controls in An Asp - Net Page
Embedding Multiple Silverlight Custom Controls in An Asp - Net Page
2. In the next window that appears select the check box “Host the silverlight application in a new
website”.
3. Right click the solution and click on “Add New Item” and select “Silverlight User Control”. Create
2 such controls – “LoginControl.xaml” and “HelloWorld.xaml”
4. Now the solution folder looks life this.
5. HelloWorld.xaml
<UserControl x:Class="MultipleUserControl.HelloWorld"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
</DoubleAnimation>
</Storyboard>
</StackPanel.Resources>
</StackPanel>
</Grid>
</UserControl>
6. HelloWorld.xaml.cs
7. LoginControl.xaml
<UserControl x:Class="MultipleUserControl.LoginControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="BurlyWood" >
<Grid.RowDefinitions >
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="lblID" Text="User ID:" Grid.Row="0" Grid.Column="0"></TextBlock>
<TextBox x:Name="txtUserID" Text="" Grid.Row="0" Grid.Column="1"></TextBox>
<TextBlock x:Name="lblPassword" Text="Password:" Grid.Row="1"
Grid.Column="0"></TextBlock>
<PasswordBox x:Name="txtPassword" Password ="" Grid.Row="1" Grid.Column="1"
PasswordChar="*"></PasswordBox>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal"
HorizontalAlignment="Right">
<Button x:Name="btnLogin" Margin="10,10,10,10" Content="Login" Width="50"
Click="btnLogin_Click"></Button>
<Button x:Name="btnCancel" Margin="10,10,10,10" Content="Cancel" Width="50"
></Button>
</StackPanel>
</Grid>
</UserControl>
8. LoginControl.xaml.cs
9. App.xaml.cs
}
10. MultipleUserControlTestPage.aspx to be modified as given below.
if (errorType == "ParserError") {
errMsg += "File: " + args.xamlFile + " \n";
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "Line: " + args.lineNumber + " \n";
errMsg += "Position: " + args.charPosition + " \n";
}
errMsg += "MethodName: " + args.methodName + " \n";
}
<div id="silverlightControlHost1">
<object id="Hello" data="data:application/x-silverlight-2," type="application/x-
silverlight-2" width="50%" height="50%">
<param name="id" value="Xaml1" />
<param name="initParams" value="ControlId=1" />
<param name="runat" value="server"/>
<param name="Source" value="ClientBin/WorkingModel.xap"/>
</object>
</div>
<div id="silverlightControlHost2">
<object id="Login" data="data:application/x-silverlight-2," type="application/x-
silverlight-2" width="50%" height="50%">
<param name="id" value="Xaml1" />
<param name="initParams" value="ControlId=2" />
<param name="runat" value="server"/>
<param name="Source" value="ClientBin/WorkingModel.xap"/>
</object>
</div>
</form>
</body>
</html>
Voila! Now when we run the MultipleUserControl.Web we get both the user controls in the
same aspx page as shown below.