关闭

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

标签: objectwpftextboxbuttonstringclass
656人阅读 评论(0) 收藏 举报
分类:
 

在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按钮后,我们可以看到如下结果:
XamlWriter

为了更清晰,我将上面结果都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”。

再想想看,这样的功能对我们有什么用途?多想多练,举一返三多得正果。

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:817805次
    • 积分:10401
    • 等级:
    • 排名:第1653名
    • 原创:190篇
    • 转载:301篇
    • 译文:1篇
    • 评论:172条
    最新评论