Silverlight call XAML at Runtime in VB.NET
In this article you will learn how to call an XAML at the runtime in silverlight.
XAML: It can initialize objects and set properties of objects, using a language structure that shows hierarchical relationships between multiple objects, and using a backing type convention that supports extension of types, it is basically embedded in your HTML pages and the Silverlight plug-in allows you to render the XAML objects onto to your page. Silverlight and XAML integrate seamlessly together. Every XAML element can be accessed or manipulated from the same client side JS that would interact with any HTML elements.
A XAML file always has exactly one element serving as its root, which declares an object that will be the conceptual root of some programming structure such as a page, or the object graph of the entire run-time definition of an application.
I will given an simple example that shows you that you can also call an XAML at the runtime:
Example of XAML
<UserControl x:Class="SilverlightApplication6.MainPage"
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:DesignWidth='640'
d:DesignHeight='480'>
<Grid x:Name="LayoutRoot" Background="White" Margin="10">
<StackPanel Orientation="Vertical">
<Canvas x:Name="Canvas" Height="321">
<Button x:Name="Button" Content="Click to Create Rectangle" Height="30"Width="189" Click="Button_Click" Canvas.Left="216" Canvas.Top="0" />
</Canvas>
</StackPanel>
</Grid>
</UserControl>
MainPage.xaml.vb Code
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Windows.Markup
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim xaml As String = "<Rectangle xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" Height=""160"" Width=""120"" Fill=""Black"" />"
Dim rectangle As Rectangle = DirectCast(XamlReader.Load(xaml), Rectangle)
Canvas.Children.Add(rectangle)
End Sub
End Class
Output Window
Conclusion
Hope you will learn through this article how you can call an XAML at runtime in silverlight.