zoukankan      html  css  js  c++  java
  • 浅谈RichTextBox在Windows Phone开发中的应用 [WP开发]

    转自:http://www.cnblogs.com/wpdev/archive/2011/09/30/2196215.html

    在Sliverlight或者WPF程序中,与Textbox相比,RichTextBox提供更为强大的功能,例如支持多种文本格式,支持图文混派,内嵌控件等等,而Windows Phone在升级到Mango(7.1)后也开始支援这个控件。现在还只是Beta版,所以在功能上还有所欠缺:

    1. 只读,还不能输入编辑;
    2. Tool box中还没有添加这个控件,只能手工创建;
    3. 没有默认样式,所以得自定义样式文件;
    4. 没有Design View实时支持。

    手动创建RichTextBox的方法有两种,一种是在XAML声明,如:

    1 <RichTextBox x:Name="rtxtBox" Margin="10" VerticalAlignment="Top">
    2                 <Paragraph FontSize="30" >
    3                     RichTextBox Demo Project
    4                 </Paragraph>
    5 </RichTextBox>

    另外一种是通过Code-Behide:

    01 RichTextBox rtb = new RichTextBox();
    02 rtb.FontSize = 30;
    03 rtb.Background = new SolidColorBrush(Colors.White);
    04 rtb.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
    05  
    06 Paragraph parag = new Paragraph();
    07 Run run = new Run();
    08 run.Foreground = new SolidColorBrush(Colors.Red);
    09 run.Text = "Red Text";
    10 parag.Inlines.Add(run);
    11 rtb.Blocks.Add(parag);
    12  
    13 ContentPanel.Children.Add(rtb);

    这里要注意,正如前面提到的,RichTextBox没有默认的样式,需要手动添加,否则不能正常显示,在App.xaml内添加自定义的样式如下:

    01 <!--Application Resources-->
    02     <Application.Resources>
    03         <Style TargetType="RichTextBox">
    04             <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
    05             <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
    06             <Setter Property="Background" Value="Transparent" />
    07             <Setter Property="BorderBrush" Value="Transparent" />
    08             <Setter Property="BorderThickness" Value="0"/>
    09             <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    10             <Setter Property="VerticalContentAlignment" Value="Center" />
    11             <Setter Property="Padding" Value="0" />
    12             <Setter Property="Template">
    13                 <Setter.Value>
    14                     <ControlTemplate TargetType="RichTextBox">
    15                         <Grid Background="Transparent">
    16                             <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="{StaticResource PhoneHorizontalMargin}">
    17                                 <ContentControl x:Name="ContentElement" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}"/>
    18                             </Border>
    19                         </Grid>
    20                     </ControlTemplate>
    21                 </Setter.Value>
    22             </Setter>
    23         </Style>
    24     </Application.Resources>

    如果想在RichTextBox内嵌入控件,可定义如下:

    01 <Paragraph>
    02                     <InlineUIContainer>
    03                         <Button Content="InlineButton"/>
    04                     </InlineUIContainer>
    05                 </Paragraph>
    06                 <Paragraph >
    07                     <InlineUIContainer>
    08                         <Image Width="80" Source="ApplicationIcon.jpg"/>
    09                     </InlineUIContainer>
    10 </Paragraph>

    之前在做Silverlight应用时,写过一个自定义的RichTextBox控件,能够保存Html基本样式,在做鲜闻阅读器时,遇到的问题也是对于Html流的处理,现在写自定义的RichTextBox Control for Windows Phone,希望能实现自己想要的转化功能。

    Microsoft视频教程

    @:ellichttp://www.cnblogs.com/libenqing
    ®: 博文是本人当时的学习笔记及知识整理,由于自身局限错误在所难免,敬请斧正.
    ©: 本文版权属于博客园和本人,版权基于署名 2.5 中国大陆许可协议发布,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接和署名ellic(包含链接),不得删节,否则保留追究法律责任的权利。

  • 相关阅读:
    通过注册表设置指定用户启动项的权限
    音频知识学习
    回老家也不忘老本......
    探索DOMNode
    表单填写示例(通过JavaScript访问DOM)
    最近的一些感悟
    C#和Java中的Substring()
    验证ASP.NET页生命周期时间的触发顺序
    解析c语言背后的汇编代码
    oracle nvl()函数在使用中出现的问题
  • 原文地址:https://www.cnblogs.com/jeekun/p/2204039.html
Copyright © 2011-2022 走看看