当前位置:编程学习 > wap >>

windows phone7.1中两个新增控件

RichTextBox
在wp7中,所有的控件都无法实现图文混排,这个控件解决了无法图文混排的问题,使微博和聊天软件不在只是文字显示那么单调了。但是这个控件目前还并不完善,只能够显示而无法进行输入,另外使用起来也比较麻烦。以下就是这个控件的使用方法:

1.       XAML中直接添加

这个控件无法显示在ToolBox列表中,需要通过手工方式来自已添加。


<RichTextBox Width="400" Height="400" Background="White" IsReadOnly="True" FontSize="30" VerticalContentAlignment="Top">

                <Paragraph>

                    <Run Foreground="Red" FontStyle="Italic" Text="Red Text"/>

                </Paragraph>

                <Paragraph Foreground="Blue">

                    <Run Text="Blue Text "/>

                    <Bold>This is bold</Bold>

                </Paragraph>

                <Paragraph Foreground="Black">

                    <Run Text="Black Text "/>

                    <Hyperlink>Click Here</Hyperlink>

                    <Bold>test Bold Text</Bold>

                </Paragraph>

                <Paragraph>

                    A RichTextBox with<Bold>initial content</Bold> in it.

                </Paragraph>

       

            </RichTextBox>
 

这样就实现了图文混排,以及多种文字效果的同时显示。

2.       代码实现


RichTextBox rtb = new RichTextBox();

            rtb.Width = 400;

            rtb.Height = 400;

            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);

 

            parag = new Paragraph();

            parag.Foreground = new SolidColorBrush(Colors.Blue);

            run = new Run();

            run.Text = "Blue Text";

            parag.Inlines.Add(run);

 

 

            Bold bold = new Bold();

            bold.Inlines.Add("This is bold Text");

            parag.Inlines.Add(bold);

 

            rtb.Blocks.Add(parag);

 

            parag = new Paragraph();

            parag.Foreground = new SolidColorBrush(Colors.Black);

            run = new Run();

            run.Text = "Black Text";

            parag.Inlines.Add(run);

 

            Hyperlink hl = new Hyperlink();

            hl.Inlines.Add("Click Here");

            parag.Inlines.Add(hl);

 

            bold = new Bold();

            bold.Inlines.Add("test Bold Text");

            parag.Inlines.Add(bold);

            run = new Run();

            run.Text = "A RichTextBox with";

            parag.Inlines.Add(run);

            bold = new Bold();

            bold.Inlines.Add("initial content");

            parag.Inlines.Add(bold);

            run = new Run();

            run.Text = "in it.";

            parag.Inlines.Add(run);

 

            rtb.Blocks.Add(parag);

 

            ContentPanel.Children.Add(rtb);
 

这两种方法实现的是同一个效果。


3.       注意事项

l 这个控件在加入到工程之前,需要在APP.xaml文件的Resource节中添加如下xaml,否则无法显示。


<Application.Resources>

        <Style TargetType="RichTextBox">

            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" />

   &

补充:移动开发 , Windows Phone ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,