在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”。
再想想看,这样的功能对我们有什么用途?多想多练,举一返三多得正果。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
求职简历-Python爬虫工程师
1. 基本信息 求职岗位:Python爬虫工程师(全职) 期望薪资:15000以上 姓名:雷坤 手机号码:18150382334 邮箱:474933858@qq.com 毕业院校:厦门大学软件学院 2.掌握技能 1.Python中8种数据类型的精通使用 2.Python匿名函数、列表推导式、装饰器的熟练使用 3.Python中re、requests、beautifulSoup等库的熟练使用 4.Python爬虫框架Scrapy的熟练使用 5.Python爬虫伪装中代理IP、UserAgent的熟练使用 6.Python与scrapyt-redis分布式爬虫的基本使用 7.Python操作Mysql数据库增删改查 8.Python操作MongoDB数据库增删改查 9.Python建立数据库连接池提高效率 10.团队协作开发工具git的熟练使用 11.熟悉Linux日常工作环境,熟练掌握常用命令和调优监控手段 12.python机器学习库scikit-learn库的熟练使用 13.python科学计算库numpy、scipy和数据分析库pandas的熟练使用 3.项目经验 1.基于Sele...
- 下一篇
7月26日云栖精选夜读丨法国队夺了世界杯冠军,却彻底打了AI预测的脸
本届世界杯,我们见证了一场场精彩的比赛,看到每个队的球员为了国家荣誉勇敢拼搏,也看到了现场的观赛的球迷,充满激情地为自己的国家加油助威。除了精彩的比赛,这届世界杯也引入了诸多科技——5G、VAR、智能设备、人工智能等,其中,AI赛事预测在世界杯的应用最为人们关注。 热点热议 法国队夺了世界杯冠军,却彻底打了AI预测的脸 作者:技术小能手 发表在:互联网er的早读课 阿里巴巴茹忆:千万级 AI 设备补贴背后的思考与布局丨WARE 2018 作者:技术小能手 发表在:深圳湾 模组厂商对话 MTK:智能音箱市场谈红海还早,下一个爆品将诞生丨WARE 2018 作者:技术小能手 发表在:深圳湾 知识整理 Python实现MySQL连接池 作者:武耀文 教你在Tableau中绘制蝌蚪图等带有空心圆的图表 作者:技术小能手 发表在:数据派THU OkHttpClient 同步、异步请求的执行流程和源码分析 作者:技术小能手 发表在:安卓巴士Android开发者门户 Redis有哪些开发设计规范值得我们注意的! 作者:技术小能手 发表在:Java后端技术 人工智能与大数据开发的12个注意点 作者:技...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8编译安装MySQL8.0.19
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题