private void OnRichTextBlockLoaded(object sender, RoutedEventArgs e) { RichTextBlockOverflow overflow = null; bool isOverFlow = false; if (this.rtTextBlock.HasOverflowContent) { overflow = new RichTextBlockOverflow(); rtTextBlock.OverflowContentTarget = overflow; this.flipview.Items.Add(overflow); overflow.UpdateLayout(); isOverFlow = overflow.HasOverflowContent; while (isOverFlow) { RichTextBlockOverflow temp = overflow; overflow = new RichTextBlockOverflow(); temp.OverflowContentTarget = overflow; this.flipview.Items.Add(overflow); overflow.UpdateLayout(); isOverFlow = overflow.HasOverflowContent; } } }
//加载文章内容,以代码处理是因为文章长度不定,即处理文本溢出问题 private void loadcontent() { stPanel.Children.Clear(); // 为了支持文本分块,使用RichTextBlock RichTextBlock tbContent = new RichTextBlock(); tbContent.Width = CT_WIDTH; tbContent.Height = CT_HEIGHT; tbContent.TextWrapping = TextWrapping.Wrap; tbContent.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); Paragraph ph = new Paragraph(); Run txtRun = new Run(); txtRun.Text = _viewmodel.one.ContentstrContent; ph.Inlines.Add(txtRun); tbContent.Blocks.Add(ph); if(Window.Current.Bounds.Height == 1080) tbContent.FontSize = Convert.ToDouble(32); else if (Window.Current.Bounds.Height == 1440) tbContent.FontSize = Convert.ToDouble(38); else tbContent.FontSize = Convert.ToDouble(20); stPanel.Children.Add(tbContent); // 更新一下状态,方便获取是否有溢出的文本 tbContent.UpdateLayout(); bool isflow = tbContent.HasOverflowContent; // 因为除了第一个文本块是RichTextBlock, // 后面的都是RichTextBlockOverflow一个一个接起来的 // 所以我们需要两个变量 RichTextBlockOverflow oldFlow = null, newFlow = null; if (isflow) { oldFlow = new RichTextBlockOverflow(); oldFlow.Width = CT_WIDTH; oldFlow.Height = CT_HEIGHT; oldFlow.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); tbContent.OverflowContentTarget = oldFlow; stPanel.Children.Add(oldFlow); oldFlow.UpdateLayout(); // 继续判断是否还有溢出 isflow = oldFlow.HasOverflowContent; } while (isflow) { newFlow = new RichTextBlockOverflow(); newFlow.Height = CT_HEIGHT; newFlow.Width = CT_WIDTH; newFlow.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); oldFlow.OverflowContentTarget = newFlow; stPanel.Children.Add(newFlow); newFlow.UpdateLayout(); // 继续判断是否还有溢出的文本 isflow = newFlow.HasOverflowContent; // 当枪一个变量填充了文本后, // 把第一个变量的引用指向当前RichTextBlockOverflow // 确保OverflowContentTarget属性可以前后相接 oldFlow = newFlow; } }