新时尚Windows8开发(31):去掉文本中的HTML标记

告诉EveryBody一个好消息,去掉字符串中的HTML标记,再也不用写正则表达式了,你知道吗?一行代码就够了!

事不宜迟,来吧,动手。

 

1、新建“板砖”应用程序项目。

2、在界面中放一个TextBox,用来输入带HTML的文本,一个Button,点击后转换,一个TextBlock,显示转换后的字符串。XAML如下:

[html]  view plain copy print ?
  1. <Page  
  2.     x:Class="App1.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:App1"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
  11.         <Grid.RowDefinitions>  
  12.             <RowDefinition/>  
  13.             <RowDefinition Height="Auto"/>  
  14.             <RowDefinition/>  
  15.         </Grid.RowDefinitions>  
  16.         <TextBox x:Name="txtInput" Grid.Row="0" TextWrapping="Wrap"/>  
  17.         <Button Grid.Row="1" Margin="5,12,0,13" Content="转换" Click="onClick"/>  
  18.         <ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Visible" HorizontalScrollMode="Enabled">  
  19.             <TextBlock x:Name="tbResult" FontSize="24" TextWrapping="Wrap"/>  
  20.         </ScrollViewer>  
  21.     </Grid>  
  22. </Page>  


3、而后我们在代码中处理Click事件。记得先引入Windows.Data.Html命名空间。

[csharp]  view plain copy print ?
  1. private void onClick(object sender, RoutedEventArgs e)  
  2. {  
  3.     this.tbResult.Text = HtmlUtilities.ConvertToText(this.txtInput.Text);  
  4. }  


 

然后,你就试试看。

新时尚Windows8开发(31):去掉文本中的HTML标记_第1张图片

你可能感兴趣的:(新时尚Windows8开发(31):去掉文本中的HTML标记)