Dynamically Loading XAML from JavaScript
<UserControl x:Class='SilverlightApplication3.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">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.415*"/>
<ColumnDefinition Width="0.585*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.15*"/>
<RowDefinition Height="0.85*"/>
</Grid.RowDefinitions>
<Button Click="Button_Click" Margin="10" Content="Click To Load XAML" />
</Grid>
</UserControl>
//File: Page.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string xamlString = "<Ellipse xmlns=\"http://schemas.microsoft.com/client/2007\" xmlns:x=\"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml\" Height=\"200\" Width=\"200\" Fill=\"Navy\" Grid.Column=\"1\" Grid.Row=\"1\" />";
UIElement element = (UIElement)XamlReader.Load(xamlString);
LayoutRoot.Children.Add(element);
}
}
}
Related examples in the same category