简体   繁体   中英

things on the usage of the richtextbox in windows phone

I'm using the richtextbox to show some Html content in windows phone 7.1.

The html source-code is like:

Paragraph1</p>
<img src="http://www.ifanr.com/wp-content/uploads/2011/11/DSC_332401.jpg" alt="" width="600" height="338" /></p>
Paragraph2。</p>
<h3>Title h3</h3>
Paragraph3。
</p>

Then I use the

"string[] sArray = Regex.Split(html, "</p>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);"

to split them into a Array. Finally, I use the code:

foreach (string array in sArray)
            {
                Paragraph parag = new Paragraph();
                Run run = new Run();
                Bold bold = new Bold();
                if (!Regex.IsMatch(array.ToString(), @"<img\b[^<>]*?\bsrc\s*=\s*[""']?\s*(?<imgUrl>[^\s""'<>]*)[^<>]*?/?\s*>"))
                {
                    //h3
                    if (array.ToString().Contains("</h3>"))
                    {
                        string hString = array.ToString();
                        hString = Regex.Replace(hString, "<h3>", "");
                        string[] hArray = Regex.Split(hString, "</h3>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                        bold.Inlines.Add(hArray[0].ToString());
                        parag.Inlines.Add(bold);
                        run.Text = hArray[1].ToString();
                        parag.Inlines.Add(run);
                    }
                    else
                    {
                        if(array.ToString().Contains("<blockquote>"))
                        {
                            run.Text = Regex.Replace(array.ToString(), "<blockquote>", "blockquote:");
                            run.FontSize = 18;
                        }
                        else
                            run.Text = array.ToString();
                        parag.Inlines.Add(run);
                    }
                    rtb.Blocks.Add(parag);
                }
                else
                {
                    //insert the image into richtextbox
                    Regex regImg = new Regex(@"http://[^\[^>]*?(gif|jpg|png|jpeg|bmp|bmp)", RegexOptions.IgnoreCase);
                    MatchCollection matches = regImg.Matches(array.ToString());
                    string result = null;
                    foreach (Match match in matches)
                        result = match.Value;

                    Image image = new Image();

                    image.Stretch = Stretch.Uniform;
                    image.Source = new BitmapImage(new Uri(result, UriKind.RelativeOrAbsolute));
                    InlineUIContainer iuc = new InlineUIContainer();
                    iuc.Child = image;
                    parag.Inlines.Add(iuc);
                    rtb.Blocks.Add(parag);
                }

to add some Paragraph or images into the richtextbox, everything goes well in the beginning, but when I Scroll down the richtextbox, the rest paragraph disappear. It confused me all day long, as I could't find out what's wrong with the richtextbox. Is it just a bug in Windows phone? Any thoughts?

ScreenShot1 :

截图1

ScreenShot2 :

截图2

ps:it doesn't matter whether the html source-code contains some non-english characters or not. This happens when the html source-code is in a large amount of words. These two ScreenShots just show the problem.

The phone applies a restriction that any UIElement can't be larger than 2048 pixels in any direction. This is enforced to avoid performance issues relating to memory and having to draw very large objects. This is to protect you from doing something that greatly affects performance but also has some other reasoning behind it. For example, a phone is a poor device for reading large pieces of text. This applies even more so for dense bodies of text. This size restriction therefore forces you to think about how, or if you should, display large pieces of text within your application.

There are some solutions though.
Rather than using a single Paragrpah or TextBlock for a large "unit" of text, you could consider using something like this: http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM