Windows phone 文本块未显示大文本

Windows phone Textblock not showing large text

如何使用 textblockrichtextbox 在 windows phone 中显示大文本字符串? 目前我面临在 textbox 其截断

中显示大量文本的问题

在 Windows Phone 上,任何单个控件的大小都有限制,这样可以避免创建需要绘制的非常大的表面,这肯定会影响性能。

有多种方法可以解决此问题,并且有一个解决方法的示例可以自动获取文本并将其分解为 http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx

处的多个 TextBlocks

如果您有更具体的示例和重现,请用这个更新您的问题。

为了解决这个问题,我们创建了一个转换器。所以首先我们应该在页面上放置一个 ContentPresenter 而不是 TextBlock 以显示我们喜欢的内容

<ContentPresenter Content="{Binding BodyText,Converter={StaticResource LongTextConverter}}" />

public string BodyText

{ 得到 { return "Lorem ipsum dolor sit amet, ..."; } }

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        UIElement result = null;

        if (value != null)
        {
            string data = value.ToString();

            if (data.Length <= MinLength)
            {
                result = new TextBlock
                             {
                                 Text = data,
                                 Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                 HorizontalAlignment = HorizontalAlignment.Stretch
                             };
            }
            else
            {
                StackPanel resultPanel = new StackPanel();
                int min = 0;
                int max = MinLength;
                while (true)
                {
                    string temp = data.Substring(min, max - min);
                    if (max != data.Length)
                    {
                        int index = temp.LastIndexOf('\n');
                        index = index == -1 ? temp.LastIndexOf('.') + 1 : -1;
                        max = (index == -1) ? max : index;
                        temp = data.Substring(min, max) + '\n';
                        resultPanel.Children.Add(new TextBlock
                                                     {
                                                         Margin = new Thickness(12, 0, 12, -30),
                                                         Text = temp,
                                                         Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                                         HorizontalAlignment = HorizontalAlignment.Stretch
                                                     });
                    }
                    else
                    {
                        resultPanel.Children.Add(new TextBlock
                                                     {
                                                         Margin = new Thickness(12, 0, 12, 0),
                                                         Text = temp,
                                                         Style = App.Current.Resources["ArticleBodyStyle"] as Style,
                                                         HorizontalAlignment = HorizontalAlignment.Stretch
                                                     });
                        break;
                    }

                    min += max;
                    max = min + MinLength;
                    if (max > data.Length)
                    {
                        max = data.Length;
                    }
                }

                result = resultPanel;
            }
        }

        return result;
    }