在WPF中,如何得到任何Object对象的XAML代码?
原文:在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"><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">WriteMyXaml_2</Button></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代码?的更多相关文章
- WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式
原文:WPF中实现PropertyGrid(用于展示对象的详细信息)的三种方式 由于WPF中没有提供PropertyGrid控件,有些业务需要此类的控件.这篇文章介绍在WPF中实现PropertyGr ...
- 使用java中的反射获得object对象的属性值
知识点:使用java中的反射获得object对象的属性值 一:场景 这两天开发代码时,调用别人的后台接口,返回值为Object对象(json形式的),我想获得object中指定的属性值,没有对应的ge ...
- WPF中桌面屏保的制作(主要代码)
原文:WPF中桌面屏保的制作(主要代码) 制作要点:(1) 使用System.Windows.Threading.DispatcherTimer;(2) 将Window属性设置为: this ...
- JavaScript中Function函数与Object对象的关系
函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...
- c#中jeson字符串和OBJECT对象的相互转换
对于本问题 我用三步来分别说明实现过程 1.定义一个类---- 实现转换的具体方法 using System; using System.Collections.Generic; using Sy ...
- C++派生类中如何初始化基类对象(五段代码)
今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...
- WPF中获取指定坐标依赖对象数据项
上图中红色框区域是一个自定义的ListBox控件,需要实现的功能是,点击红框区域中某项时,获取当前选中项的数据项 控件的MouseDown事件部分代码为: var x = TreeHelper.Fin ...
- WPF中,怎样将XAML代码加载为相应的对象?
原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...
- WPF中任意Object的XAML代码格式化输出
原文:WPF中任意Object的XAML代码格式化输出 有时候,我们需要将WPF中的控件自身的XAML代码输出成文本,那么,我们可以使用System.Windows.Markup.XamlWriter ...
随机推荐
- linux中竖线'|',双竖线‘||’,&和&&的意思
对于初学者来说这几个意思可能只知道其中几个的意思,下面我们来看一下. 1.竖线'|' ,在linux中是作为管道符的,将'|'前面命令的输出作为'|'后面的输入.举个例子 [18066609@root ...
- ORA-00907: 缺失右括号问题整理解决
ORA-00907: 缺失右括号 前言 近期在开发过程中使用oracle数据库,在程序中进行查询数据时遇到了"ORA-00907: 缺失右括号"的问题.可是假设直接把sql语句直接 ...
- 编辑器vim简介
vi简介 vi是"Visual interface"的简称,它在Linux上的地位就仿佛Edit程序在DOS上一样.它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且用户 ...
- [Ramda] Curry and Uncurry Functions with Ramda
Most of the functions offered by the ramda library are curried by default. Functions you've created ...
- 【u120】红牌
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括N个步骤.每一步骤 ...
- 联想笔记本装win7
http://jingyan.baidu.com/article/7c6fb42862d63a80642c90b8.html http://bbs.thinkworldshop.com.cn/thre ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 使用apidoc 生成Restful web Api文档
在项目开发过程中,总会牵扯到接口文档的设计与编写,之前使用的都是office工具,写一个文档,总也是不够漂亮和直观.好在git上的开源大神提供了生成文档的工具,so来介绍一下! 该工具是Nodejs的 ...
- 免费的 C/C++ 编译&解释 器列表
摘自<C++编程网>,详细介绍请参考http://www.cpp-prog.com/2009/0520/118.html MicrosoftVisual C++ 2008 Express ...
- [GeekBand] STL 仿函数入门详解
本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格 http://www.leavesite. ...