设为首页 收藏本站
查看: 484|回复: 0

解决windows phone 7中TextBlock只文字显示不完全的问题

[复制链接]

尚未签到

发表于 2015-5-10 16:46:14 | 显示全部楼层 |阅读模式
  顺利解决textblock显示文字不完全的问题:
  There could be many scenarios when you'd need to be able to dipslay a text in your WP7 application which doesn't fit on the screen. The easiest way to approach to solve this would be to utilize the ScrollViewer control as a host for a TextBlock. Something like that:


      
            
      

  You would think that we're done but as usual the "devil is in the details". If you assign a long text to the TextBlock you will see that part of this text could be truncated and an empty space would be displayed when scrolling to the end:
http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-86-10/4188.empty_5F00_scroll.PNG
  The reason for this behavior is that any element that must be displayed beyond the area which is larger than 2048x2048 pixels would be clipped by the platform. I was told that this limitation had been dictated by a combination of hardware limitation and performance considerations. To workaround this limitation we'd need to break out the text into a separate blocks and create a TextBlock for each of this text blocks. So to make the life easier for myself and for you I created a custom control which I called ScrollableTextBlock which wraps this logic. So I created a custom control which derives from Control and implements Text property:

    public class ScrollableTextBlock : Control
    {
        private StackPanel stackPanel;
        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);
        }
  }
  And here's the ParseText method which is also a part of this control:

    private void ParseText(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;
             }
             // Pickup the leftover text
             if (value.Length % maxTexCount > 0)
             {
                 TextBlock textBlock = this.GetTextBlock();
                 textBlock.Text = value.Substring(maxTexCount * n, value.Length - maxTexCount * n);
                 this.stackPanel.Children.Add(textBlock);                  
             }
         }
     }
  In the code above I measure the text length and if it's greater that maxTextCount I break the text into a separate blocks and create a new TextBlock with this text. After the TextBlock is created I add it to the StackPanel. To make a picture complete here's how the control's style looks like:

   
        
        
        
        
        
        
        
            
               
                    
                        
                    
               
            
        
   
  As you can see, the StackPanel is located inside of the ScrollViewer control which provides the scrolling functionality.  And after we execute the test project the result is that the text is not truncated:
http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-86-10/4087.full_5F00_scroll.PNG
  原文作者:Alex Takhnin's Blog

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65594-1-1.html 上篇帖子: 在Windows XP上安装Windows Phone 7教程 下篇帖子: Windows 7旗舰版搭建andriod 4.0开发环境记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表