原文: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
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();             //LoadEmbeddedXaml();
            //LoadEmbeddedXaml2();
            LoadEmbeddedXaml3();
        }         //从字符串中加载
        public void LoadEmbeddedXaml() 
        { 
            Title = "Load Embedded Xaml";
            string strXaml = "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'" + 
                " Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'>" + 
                " From String Object!</Button>"; 
            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);
        }
    } 
}
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'    Foreground='LightSeaGreen' 
    FontSize='16pt' 
    Width='128' 
    Height='32'>
    From File Object!
</Button>

WPF中动态加载XAML中的控件的更多相关文章

  1. 在ASP.NET中动态加载内容(用户控件和模板)

    在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...

  2. [WPF学习笔记]动态加载XAML

    好久没写Blogs了,现在在看[WPF编程宝典],决定开始重新写博客,和大家一起分享技术. 在编程时我们常希望界面是动态的,可以随时变换而不需要重新编译自己的代码. 以下是动态加载XAML的一个事例代 ...

  3. 某APK中使用了动态注册BroadcastReceiver,Launcher中动态加载此APK出现java.lang.SecurityException异常的解决方法

    在某APK中,通过如下方法动态注册了一个BroadcastReceiver,代码参考如下: @Override protected void onAttachedToWindow() { super. ...

  4. 在MVC应用程序中动态加载PartialView

    原文:在MVC应用程序中动态加载PartialView 有时候,我们不太想把PartialView直接Render在Html上,而是使用jQuery来动态加载,或是某一个事件来加载.为了演示与做好这个 ...

  5. 在VC中动态加载ODBC的方法

    在使用VC.VB.Delphi等高级语言编写数据库应用程序时,往往需要用户自己在控制面板中配置ODBC数据源.对于一般用户而言,配置ODBC数据源可能是一件比较困难的工作.而且,在实际应用中,用户往往 ...

  6. vue中动态加载img

    想实现动态加载图片,当点击“首页”时,图片变色 代码如下: <mt-tabbar v-model="selected" fixed class="border-1p ...

  7. WPF 3D动态加载模型文件

    原文:WPF 3D动态加载模型文件 这篇文章需要读者对WPF 3D有一个基本了解,至少看过官方的MSDN例子. 一般来说关于WPF使用3D的例子,都是下面的流程: 1.美工用3DMAX做好模型,生成一 ...

  8. XamlReader 动态加载XAML

    原文:XamlReader 动态加载XAML XAML: <Grid xmlns:x="http://schemas.microsoft.com/client/2006" x ...

  9. 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)

    实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...

随机推荐

  1. proxool数据库连接池用法

    今天给大家介绍一种新的数据连接池实现方式--proxool数据库连接池,这是一个健壮.易用的连接池.以下通过一个Demo说明一下怎样使用: 项目结构例如以下: DBLink.java文件里的代码: p ...

  2. [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js

    You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...

  3. Dll的链接使用细节

    关于Dll Dll.Exe 都是PE格式的二进制文件. Dll相当于Linux操作系统下的so文件 1 基地址(Base Address)和相对地址(RelativeVirtual Address) ...

  4. POJ 1833 生成排列

    题目链接:POJ 1833 /************************************ * author : Grant Yuan * time : 2014/10/19 16:38 ...

  5. Java反射学习总结一(基础篇)

    Class类是Reflection API中核心的类,他位于Java.lang.Class 列出一些常用的方法. - getName() : 获得类的完整名字 - getFields() : 获得类的 ...

  6. [RxJS] Replace zip with combineLatest when combining sources of data

    This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. ...

  7. java异常中Exception捕获不到的异常

    一 概念 众所周知java提供了丰富的异常类,这些异常类之间有严格的集成关系,分类为 父类Throwable Throwable的两个子类Error和Exception Exception的两个子类C ...

  8. ios开发之图层与核心动画一:图层CALayer的认识

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  9. windows phone8.1:页面导航详解

    小梦给大家带来windows phone 8.1应用开发实战教程,分享自己学习,开发过程中的经验和技巧. 今天给大家分享windows phone 8.1页面导航相关知识.涉及知识点如下: 页面一导航 ...

  10. 关于CoordinatorLayout与Behavior的一点分析

    Behavior是Android新出的Design库里新增的布局概念.Behavior只有是CoordinatorLayout的直接子View才有意义.可以为任何View添加一个Behavior.Be ...