原文:在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. .NET Framework基础知识(二)(转载)

    1.课外:为什么object数组可以赋值给object类型,int数组却不能赋值给int类型? 答:因为不管是什么什么数组都继承自Array,而Array又继承自object. 2.线程:是操作系统分 ...

  2. 【u016】无序字母对

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个 ...

  3. Spark源代码阅读笔记之DiskStore

    Spark源代码阅读笔记之DiskStore BlockManager底层通过BlockStore来对数据进行实际的存储.BlockStore是一个抽象类,有三种实现:DiskStore(磁盘级别的持 ...

  4. Guava中TreeRangeMap基本使用

    RangeMap跟一般的Map一样.存储键值对,依照键来取值.不同于Map的是键的类型必须是Range,也既是一个区间.RangeMap在Guava中的定义是一个接口: public interfac ...

  5. jQuery实现复选框的全选、反选、并且根据复选框的<checked属性>控制多个对应div的显示/隐藏

    <!doctype html><html> <head> <meta charset="utf-8"> <title>j ...

  6. 一起学Python:字典介绍

    字典介绍 想一想: 如果有列表 nameList = ['xiaoZhang', 'xiaoWang', 'xiaoLi']; 需要对"xiaoWang"这个名字写错了,通过代码修 ...

  7. Arcgis api for javascript学习笔记-控制地图缩放比例尺范围(3.2X版本与4.6版本)

    Ⅰ. 在3.X版本中,设置Map对象的 "maxScale" 和 "minScale" 属性 <!DOCTYPE html> <html> ...

  8. Indexing Sensor Data

    In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set o ...

  9. docker入门1: Dockerfile介绍

    Dockerfile是为快速构建docker image而设计的,当你使用dockerbuild 命令的时候,docker 会读取当前目录下的命名为Dockerfile(首字母大写)的纯文本文件并执行 ...

  10. VS2010设置VC6的字体样式及背景色、选中字高亮

    习惯了VC6.0的fixedsys字体,用VS2010还真不习惯.把VS2010打造成经典的.熟悉的模样,也并非难事.网上有相应的文章,我再记录下来,主要是为了自己查找方便(刚刚重装了系统,一切从头再 ...