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

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在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">&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代码?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 WPF / XAML 项目中使用 Rive 动画?

WPF后台如何获得前台XAML中对象

如何在 WPF 窗口上加载 Xaml 文件?

如何在设计时避免 XAML 代码中的“对象引用未设置为对象的实例”异常?

wpf中,如何引用其他xaml文件中的Resources

wpf 窗体中如何取得xaml的路径