惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
C
Check Point Blog
IT之家
IT之家
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
Last Week in AI
Last Week in AI
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
B
Blog
T
The Blog of Author Tim Ferriss
H
Help Net Security
云风的 BLOG
云风的 BLOG

博客园 - 挑战

从存储过程中读取相关信息 Blend Step by Step书籍笔记(第一章) WPF非轮询方式实时更新数据库变化SqlDependency 解决为'*********' 的游标已存在问题 数据表死锁查询和处理 SQL Server操作XML(六)XML FLOWR SQL Server操作XML(五)XML Query-XQuery SQL Server操作XML(四)XML数据类型 SQL Server操作XML(三)OPENXML函数功能 SQL Server操作XML(二)XML子句实例 SQL Server操作XML(一)XML子句 数据绑定 最为详尽的WPF类继承关系 LinQ数据访问 WPF Diagram Designer Part 3:连接Item 照猫画虎WPF之二数据绑定 照猫画虎WPF之一:命名空间 解决WPF部署后客户端访问安全性问题 C#读取文本播放相应语音
动态解析XAML文本构建WPF的UI
挑战 · 2012-07-07 · via 博客园 - 挑战

 (1)手工拼凑字符串

      string strXaml = @"<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Content='Button' Height='23' Name='button1' Width='75' />";
      StringReader readerXaml = new StringReader(strXaml);
      XmlTextReader xtrXaml = new XmlTextReader(readerXaml);
      object objXaml = XamlReader.Load(xtrXaml);
      this.Content = objXaml;          

      注意:

      1.为了便于编辑,将""改为''

      2.Button元素必须指定命名空间,否则XamlReader无法辨识其类型,报错

 (2)读取已有文档

            string strXaml = "";
            Object dataObject = new Object();
            Uri uri = new Uri(@"/DepartmentElement.xaml", UriKind.Relative);
            StreamResourceInfo info = Application.GetResourceStream(uri);
            using (StreamReader sr = new StreamReader(info.Stream))
            {
                strXaml = sr.ReadToEnd();
            }
            StringReader readerXaml = new StringReader(strXaml);
            XmlTextReader xtrXaml = new XmlTextReader(readerXaml);
            object objXaml = XamlReader.Load(xtrXaml);
            this.Content = objXaml;

        注意:已有文档的BuildAction必须设置为Resource而不是Page,否则XmlTextReader 将其解析为十六进制字符串,而无法加载