WPF实现html转换xaml,效果和程序如下:
<Window x:Class="HtmlToXamlDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HtmlToXamlDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Border Background="LightSteelBlue" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"
BorderBrush="DarkSlateGray" BorderThickness="1">
<TabControl Margin="5">
<TabItem Header="HTML to XAML">
<StackPanel Margin="10">
<TextBlock Margin="10" TextWrapping="Wrap">Paste HTML Content in the TextBox below, then press the "Convert" button to convert the HTML to a XAML FlowDocument.</TextBlock>
<TextBox Margin="10" Name="myTextBox" BorderBrush="DarkSlateBlue" HorizontalAlignment="Stretch"
MinHeight="200" MaxHeight="400" xml:space="preserve" AcceptsReturn="True" />
<StackPanel Orientation="Horizontal" Margin="5">
<Button Click="ConvertContent" Margin="5">Convert!</Button>
<Button Click="CopyXaml" Margin="5">Copy to Clipboard</Button>
</StackPanel>
</StackPanel>
</TabItem>
<TabItem Header="XAML to HTML">
<StackPanel Margin="10">
<TextBlock Margin="10" TextWrapping="Wrap">Paste FlowDocument (XAML) Content in the TextBox below, then press the "Convert" button to convert the FlowDocument to HTML.</TextBlock>
<TextBox Margin="10" Name="myTextBox2" BorderBrush="DarkSlateBlue" HorizontalAlignment="Stretch"
MinHeight="200" MaxHeight="400" xml:space="preserve" AcceptsReturn="True" />
<StackPanel Orientation="Horizontal" Margin="5">
<Button Click="ConvertContent2" Margin="5">Convert!</Button>
<Button Click="CopyHtml" Margin="5">Copy to Clipboard</Button>
</StackPanel>
</StackPanel>
</TabItem>
</TabControl>
</Border>
</Window>