WPFでのXAMLの動的ロードコントロールのテスト


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Windows.Markup;

/*
 *  : WPF XAML 
 *        。
 *  :Kagula
 *  :2012-09-20
 *  :VS2008 .NET FRAMEWORK 3.5 
 *  :[1]《Application=Code+Markup   19》
 *               http://space.itpub.net/15123181/viewspace-423015
 *           [2]《Pack URIs in Windows Presentation Foundation》
 *               http://technet.microsoft.com/en-US/library/aa970069(v=vs.90)
 */
namespace testXAMLLoad
{
    /// 
    /// Interaction logic for Window1.xaml
    /// 
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            //LoadEmbeddedXaml();
            //LoadEmbeddedXaml2();
            LoadEmbeddedXaml3();
        }

        // 
        public void LoadEmbeddedXaml() 
        { 
            Title = "Load Embedded Xaml";
            string strXaml = ""; 
            StringReader strreader = new  StringReader(strXaml); 
            XmlTextReader xmlreader = new  XmlTextReader(strreader);
            object obj = XamlReader.Load(xmlreader);
            grid1.Children.Add((UIElement)obj);
        }

        //  Button 
        public void LoadEmbeddedXaml2()
        {
            XmlTextReader xmlreader = new XmlTextReader("d:\\a.xaml");
            UIElement obj = XamlReader.Load(xmlreader) as UIElement;
            grid1.Children.Add((UIElement)obj);
        }

        // 
        public void LoadEmbeddedXaml3()
        {
            //Build Action = Resource,Do not Copy, cs 
            Uri uri = new Uri("/LoadXamlResource.xaml",UriKind.Relative);
            Stream stream =Application.GetResourceStream(uri).Stream;
            //FrameworkElement UIElement
            FrameworkElement obj =XamlReader.Load(stream) as FrameworkElement;
            grid1.Children.Add(obj);
        }
    } 
}

xamlファイルのリスト