在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 ...
随机推荐
- 【23.58%】【code forces 321E】Ciel and Gondolas
time limit per test4 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- PHP CodeBase: 判断用户是否手机访问
随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.这里就介绍 ...
- 浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构)
浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构) 一.总结 1.BOM操作所有和浏览器相关的东西:网页文档dom,历史记录,浏览器屏幕,浏览器信息,文档的地址url,页面的框架集. ...
- 通过rinetd实现port转发来訪问内网的服务
一. 问题描写叙述 通过外网来訪问内网的服务 二. 环境要求 须要有一台能够外网訪问的机器做port映射.通过数据包转发来实现外部訪问阿里云的内网服务 三. 操作方法 做port映射的方案 ...
- 如何在使Xcode打包iOS应用时自动增加编译号
在红框标注的输入框中输入:真机调试编译成功增加 echo $CONFIGURATION if [ "Release" == "${CONFIGURATION}" ...
- hello.c内核模块编译 -- linux内核
Linux开发模块,在本机上看调试信息的方法走通了.当前版本号2.6.32-32-generic uname –r 能够查询 这里取module_param()作为样例. 该宏被定义在include/ ...
- 一起学Python:协程
一:协程-yield 协程,又称微线程,纤程.英文名Coroutine. 协程是啥 协程是python个中另外一种实现多任务的方式,只不过比线程更小占用更小执行单元(理解为需要的资源). 为啥说它是一 ...
- 【a803】营救
Time Limit: 10 second Memory Limit: 2 MB 问题描述 铁达尼号遇险了!它发出了求救信号.距离最近的哥伦比亚号收到了讯息,时间就是生命,必须尽快赶到那里.通过侦测, ...
- 在CSDN博客中添加量子恒道统计功能的做法
作者:朱金灿 来源:http://blog.csdn.net/clever101 什么是量子恒道统计?量子恒道统计是一套免费的网站流量统计分析系统.致力于为所有个人站长.个人博主.所有网站管理者.第三 ...
- WPF MeshGeometry3D
说说 MeshGeometry3D 里 常用的 四个属性. 先看看 MSDN 的 简介 先说说 Positions,介绍说 是顶点位置的集合,什么意思,看张图片. 这张简单描述了一个三位坐标系,里面有 ...