Tuesday 19 June 2012

Loading Xaml Dynamically in silverlight 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Windows.Markup;

namespace slXamlLoad
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            
            string buttonxaml;
      
            StringBuilder sb1 = new StringBuilder();

            sb1.Append("<Button  xmlns='http://schemas.microsoft.com/client/2007'   Content='Button' Height='23' HorizontalAlignment='Left' Margin='10,10,0,0' Name='button1' VerticalAlignment='Top' Width='75' />");

            buttonxaml = sb1.ToString(0, sb1.Length);
            Button mybutton = XamlReader.Load(buttonxaml) as Button;

            LayoutRoot.Children.Add(mybutton);
            
        }
    }
}

                             
The Xaml Code for the above is as follows
<UserControl x:Class="slXamlLoad.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:DesignHeight="315" d:DesignWidth="730">
    
    
       
    

No comments:

Post a Comment