原文:在WPF中,如何得到任何Object对象的XAML代码?

在WPF中,可以使用System.Windows.Markup.XamlWriter.Save(objName)得到任何Object对象的XAML代码。

这里举个例子,然后来比较一下:
XAML代码:
// Window1.xaml
<Window x:Class="XamlWriter.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="XamlWriter" Height="421" Width="485"
    >
    <Grid Name="Grid1">
    <Button Height="23" Margin="9,13,0,0" Name="buttonA" VerticalAlignment="Top" Click="WriteXaml_A" HorizontalAlignment="Left" Width="92">WriteMyXaml_1</Button>
    <Button Height="23" Margin="119,14,0,0" Name="buttonB" VerticalAlignment="Top"  Click="WriteXaml_B" HorizontalAlignment="Left" Width="96">WriteMyXaml_2</Button>
    <Button Height="24" Margin="228,15,141,0" VerticalAlignment="Top"  Name="buttonC" Click="WriteGridXaml">WriteGridXaml</Button>
    <Button Height="23" HorizontalAlignment="Right" Margin="0,15,11,0" VerticalAlignment="Top" Width="115" Click="WriteCSharpCode">WriteCodeButton</Button>
    <TextBox Margin="9,50,10,1" Name="textBox1" TextWrapping="Wrap"></TextBox>
  </Grid>
</Window>

C#代码:
// Window1.xaml.cs
using System;
using System.Collections.Generic;
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.Shapes;

namespace XamlWriter
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void WriteXaml_A(object sender, RoutedEventArgs e)
        {
            string savedButton = System.Windows.Markup.XamlWriter.Save(this.buttonA);
            textBox1.Text = savedButton;
        }

        private void WriteXaml_B(object sender, RoutedEventArgs e)
        {
            string savedButton = System.Windows.Markup.XamlWriter.Save(this.buttonB);
            textBox1.Text = savedButton;
        }

        private void WriteGridXaml(object sender, RoutedEventArgs e)
        {
            string savedButton = System.Windows.Markup.XamlWriter.Save(this.Grid1);
            textBox1.Text = savedButton;
        }

        private void WriteCSharpCode(object sender, RoutedEventArgs e)
        {
            Button origianlButton = new Button();
            origianlButton.Height = 50;
            origianlButton.Width = 100;
            origianlButton.Background = Brushes.AliceBlue;
            origianlButton.Content = "Click Me";

            string savedButton = System.Windows.Markup.XamlWriter.Save(origianlButton);
            textBox1.Text = savedButton;
        }

    }
}

运行程序,当点击WriteGridXaml按钮后,我们可以看到如下结果:

为了更清晰,我将上面结果都COPY成文字,为了方便阅读,我做了适当整理(加了换行):
<Grid Name="Grid1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Button Height="23" Margin="9,13,0,0" Width="92" HorizontalAlignment="Left" Name="buttonA" VerticalAlignment="Top">WriteMyXaml_1</Button>
<Button Height="23" Margin="119,14,0,0" Width="96" HorizontalAlignment="Left" Name="buttonB" VerticalAlignment="Top">WriteMyXaml_2</Button>
<Button Height="24" Margin="228,15,141,0" Name="buttonC" VerticalAlignment="Top">WriteGridXaml</Button>
<Button Height="23" Margin="0,15,11,0" Width="115" HorizontalAlignment="Right" VerticalAlignment="Top">WriteCodeButton</Button>
<TextBox TextWrapping="Wrap" Margin="9,50,10,1" Name="textBox1" AcceptsReturn="True">&lt;Button Height="23" Margin="119,14,0,0" Width="96" HorizontalAlignment="Left" Name="buttonB" VerticalAlignment="Top" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;WriteMyXaml_2&lt;/Button&gt;</TextBox>
</Grid>

我们来对比一下最原始的XAML代码与我们得到的XAML代码,为了简洁,只选第一个名为“buttonA”的按钮。

原始的XAML代码(从window1.xaml中节选):
<Button Height="23" Margin="9,13,0,0" Name="buttonA" VerticalAlignment="Top" Click="WriteXaml_A" HorizontalAlignment="Left" Width="92">WriteMyXaml_1</Button>

使用XamlWriter.Save()得到的XAML代码:
<Button Height="23" Margin="9,13,0,0" Width="92" HorizontalAlignment="Left" Name="buttonA" VerticalAlignment="Top">WriteMyXaml_1</Button>

请注意比较,有何不同?是不是Button的属性排列次序有变?而且,Click="WriteXaml_A" 这样的代码没有了?

其他的我也不多说了,想想看为什么?

运行WriteCSharpCode(object sender, RoutedEventArgs e)后会得到些什么呢?以下是结果:
<Button Height="50" Width="100" Background="#FFF0F8FF" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">Click Me</Button>

而其C#是:
Button origianlButton = new Button();
origianlButton.Height = 50;
origianlButton.Width = 100;
origianlButton.Background = Brushes.AliceBlue;
origianlButton.Content = "Click Me";

这就是C# 代码与XAML代码的相互转换了。提示:留意Background属性那句,将Brushes.AliceBlue转换成了“#FFF0F8FF”。

再想想看,这样的功能对我们有什么用途?多想多练,举一返三多得正果。

在WPF中,如何得到任何Object对象的XAML代码?的更多相关文章

  1. WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式

    原文:WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式 由于WPF中没有提供PropertyGrid控件,有些业务需要此类的控件.这篇文章介绍在WPF中实现PropertyGr ...

  2. 使用java中的反射获得object对象的属性值

    知识点:使用java中的反射获得object对象的属性值 一:场景 这两天开发代码时,调用别人的后台接口,返回值为Object对象(json形式的),我想获得object中指定的属性值,没有对应的ge ...

  3. WPF中桌面屏保的制作(主要代码)

    原文:WPF中桌面屏保的制作(主要代码) 制作要点:(1) 使用System.Windows.Threading.DispatcherTimer;(2) 将Window属性设置为:      this ...

  4. JavaScript中Function函数与Object对象的关系

    函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...

  5. c#中jeson字符串和OBJECT对象的相互转换

    对于本问题   我用三步来分别说明实现过程 1.定义一个类---- 实现转换的具体方法 using System; using System.Collections.Generic; using Sy ...

  6. C++派生类中如何初始化基类对象(五段代码)

    今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...

  7. WPF中获取指定坐标依赖对象数据项

    上图中红色框区域是一个自定义的ListBox控件,需要实现的功能是,点击红框区域中某项时,获取当前选中项的数据项 控件的MouseDown事件部分代码为: var x = TreeHelper.Fin ...

  8. WPF中,怎样将XAML代码加载为相应的对象?

    原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...

  9. WPF中任意Object的XAML代码格式化输出

    原文:WPF中任意Object的XAML代码格式化输出 有时候,我们需要将WPF中的控件自身的XAML代码输出成文本,那么,我们可以使用System.Windows.Markup.XamlWriter ...

随机推荐

  1. linux java配置环境变量

    export JAVA_HOME=/alidata/server/java/jdk1.8.0_65export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME ...

  2. springMVC注解@initbinder

    在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 比 ...

  3. [tmux] Handle history in tmux sessions

    In this lesson, we'll look at how to manage your history between tmux sessions, and ensure that your ...

  4. Android 升级到Android Studio2.2 后打不开以前版本的项目

    1.找到 build.gradle 用记事本打开,修改如下: // Top-level build file where you can add configuration options commo ...

  5. ArcEngine创建IElement简单例子

    转自IT-GIS终结者原文ArcEngine创建IElement简单例子 代码下载地址:http://files.cnblogs.com/ogis/MapControlApplication2.rar ...

  6. 【t034】Matrix67的派对

    Time Limit: 1 second Memory Limit: 1 MB [问题描述] Matrix67发现身高接近的人似乎更合得来.Matrix67举办的派对共有N(1<=N<=1 ...

  7. VC++中用API调用对话框资源

    关键技术: 对于资源的载入须要几个API函数,以下分别介绍这几个API函数. a) FindResource 用来在一个指定的模块中定位所指的资源,语法例如以下: HRSRC FindResource ...

  8. Qt单元测试工具 QTestlib(QVERIFY, QFETCH, QCOMPARE等)

    优点: QTestLib提供了单元测试框架的基本功能,并提供了针对GUI测试的扩展功能. 特性 详细描述 轻量级 QTestlib 只包含 6000行代码和 60个导出符号. 自包含 对于非GUI测 ...

  9. Curator简介

    Curator简介 Curator是一个博物馆或者其它集合的监管者或者管理者,Curator有以下几个组件组成: Recipes: 实现了通用ZooKeeper的recipes, 该组件是在Frame ...

  10. Spring Boot 定制与优化内置的Tomcat容器

    1.Spring Boot定制与优化内置Tomcat容器. > 内置的容器有三个分别是Undertow.Jetty.Tomcat,Spring Boot 对这三个容器分别进行了实现,它们上层接口 ...