wp7超大文本显示控件 垂直滚动
wp7中textblock显示有文本数量限制。貌似超过2048个字符就不显示了。
从国外网站找了一个控件,实现加载大量文本显示,并且垂直滚动浏览。
实现原理其实挺简单的,就是动态创建多个textblock控件,每个控件显示部分内容。
有点,加载大量文本时,垂直方向滚动浏览,用户体验度较高(个人感觉比一页页翻页舒服多了)
缺点,由于加载了多个控件,所以加载速度慢(与文本长度有关),这也许时微软限制显示数量的原因。
上代码:
[csharp]
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
namespace wwb.Phone.Ctrls
{
public class ScrollableTextBlock : Control
{
private StackPanel stackPanel;
private TextBlock measureBlock;
public ScrollableTextBlock()
{
// Get the style from generic.xaml
this.DefaultStyleKey = typeof(ScrollableTextBlock);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(ScrollableTextBlock),
new PropertyMetadata("ScrollableTextBlock", OnTextPropertyChanged));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ScrollableTextBlock source = (ScrollableTextBlock)d;
string value = (string)e.NewValue;
source.ParseText(value);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.stackPanel = this.GetTemplateChild("StackPanel") as StackPanel;
this.ParseText(this.Text);
}
private void ParseTextHide(string value)
{
if (this.stackPanel == null)
{
return;
}
// Clear previous TextBlocks
this.stackPanel.Children.Clear();
// Calculate max char count
int maxTexCount = this.GetMaxTextSize();
if (value.Length < maxTexCount)
{
TextBlock textBlock = this.GetTextBlock();
textBlock.Text = value;
this.stackPanel.Children.Add(textBlock);
}
else
{
int n = value.Length / maxTexCount;
int start = 0;
// Add textblocks
for (int i = 0; i < n; i++)
{
TextBlock textBlock = this.GetTextBlock();
textBlock.Text = value.Substring(start, maxTexCount);
this.stackPanel.Children.Add(textBlock);
start = maxTexCount;
}
&nb
补充:移动开发 , Windows Phone ,