关于Windows Phone中RichTextBox的使用的一些事情
我正在使用 richtextbox 在 Windows Phone 7.1 中显示一些 Html 内容。
html 源代码如下:
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>
然后我使用
"string[] sArray = Regex.Split(html, "</p>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);"
将它们拆分为数组。最后,我使用代码:
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);
}
将一些段落或图像添加到richtextbox中,一开始一切顺利,但是当我向下滚动richtextbox时,其余段落消失。它让我一整天都困惑,因为我无法找出 Richtextbox 出了什么问题。 这只是 Windows Phone 中的一个错误吗?有什么想法吗?
ScreenShot1:
ScreenShot2:
ps:html 源代码是否包含一些非英文字符并不重要。当 html 源代码包含大量单词时,就会发生这种情况。这两个屏幕截图只是显示了问题。
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:
ScreenShot2:
p.s: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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
手机应用了一个限制,即任何
UIElement
在任何方向上都不能大于 2048 像素。强制执行此操作是为了避免与内存相关的性能问题以及必须绘制非常大的对象。这是为了防止您做一些会极大影响性能但背后还有其他原因的事情。例如,手机不太适合阅读大段文本。对于密集的文本体来说更是如此。因此,这种大小限制迫使您考虑如何或是否应该在应用程序中显示大段文本。不过有一些解决方案。
您可以考虑使用类似这样的内容,而不是对大型文本“单元”使用单个
Paragrpah
或TextBlock
: msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx" rel="noreferrer">http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspxThe 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
orTextBlock
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