扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
这里举个例子,然后来比较一下:
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 { /// /// Interaction logic for Window1.xaml /// 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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者