浅谈RichTextBox在Windows Phone开发中的应用 - [WP开发]

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

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

 

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

<RichTextBox x:Name="rtxtBox" Margin="10" VerticalAlignment="Top">
                <Paragraph FontSize="30" >
                    RichTextBox Demo Project
                </Paragraph>
</RichTextBox>

另外一种是通过Code-Behide:

            RichTextBox rtb = new RichTextBox();
            rtb.FontSize = 30;
            rtb.Background = new SolidColorBrush(Colors.White);
            rtb.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;

            Paragraph parag = new Paragraph();
            Run run = new Run();
            run.Foreground = new SolidColorBrush(Colors.Red);
            run.Text = "Red Text";
            parag.Inlines.Add(run);
            rtb.Blocks.Add(parag);
 
            ContentPanel.Children.Add(rtb);

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

<!--Application Resources-->
    <Application.Resources>
        <Style TargetType="RichTextBox">
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RichTextBox">
                        <Grid Background="Transparent">
                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Margin="{StaticResource PhoneHorizontalMargin}">
                                <ContentControl x:Name="ContentElement" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>

 

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

<Paragraph>
                    <InlineUIContainer>
                        <Button Content="InlineButton"/>
                    </InlineUIContainer>
                </Paragraph>
                <Paragraph >
                    <InlineUIContainer>
                        <Image Width="80" Source="ApplicationIcon.jpg"/>
                    </InlineUIContainer>
</Paragraph>

 

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

 

Microsoft视频教程

posted @ 2011-09-30 11:13  卿之  阅读(1667)  评论(5编辑  收藏  举报
无觅相关文章插件,快速提升流量