C# 从ViewModel向可视树添加RichTextBlockOverflow
我正在构建一个电子阅读器类型的应用程序作为Windows8.1/WP8.1通用应用程序。我的文本包含在C# 从ViewModel向可视树添加RichTextBlockOverflow,c#,windows-runtime,windows-store-apps,richtextblock,richtextblockoverflow,C#,Windows Runtime,Windows Store Apps,Richtextblock,Richtextblockoverflow,我正在构建一个电子阅读器类型的应用程序作为Windows8.1/WP8.1通用应用程序。我的文本包含在FlipView中,该视图使用ObservableCollection作为其源。每个FrameworkElement都是RichTextBlock或RichTextBlockOverflow。我的问题是FlipView在第三个元素之后会停止显示任何内容 我发现有人带着一把枪。他们的解决方案似乎表明问题在于RichTextBlock/Overflows不在可视化树中。但是,当我试图通过手动将Ric
FlipView
中,该视图使用ObservableCollection
作为其源。每个FrameworkElement
都是RichTextBlock或RichTextBlockOverflow。我的问题是FlipView
在第三个元素之后会停止显示任何内容
我发现有人带着一把枪。他们的解决方案似乎表明问题在于RichTextBlock/Overflows
不在可视化树中。但是,当我试图通过手动将RichTextBlock/Overflows
附加到层次结构中更高的StackPanel
来实现相同的操作时,我会收到一个异常,告诉我该元素已经是某个元素的子元素(它是-,它是它被添加到的observetecollection
的子元素)
我怎样才能解决这个问题?我的RichTextBlock及其溢出不应该已经是树的一部分,因为它们在FlipView的ItemSource中吗
我的ViewModel代码如下。我可以通过命令从我的角度访问元素,尽管我希望将其保持在最低限度
编辑我已经用解决方法“修复”了这个问题。这里的问题是FlipView虚拟化了它的子元素,因此在滚动到离第一页足够远的地方后,原始的RichTextBlock被虚拟化了,这会导致依赖它的所有RichTextBlockOverflow丢失其内容。我的解决方案是将FlipView的ItemsPanelTemplate从VirtualzingStackPanel更改为StackPanel。这里明显的缺点是我失去了虚拟化的性能优势。我想我会将此作为一个自我回答,除非我很快找到或收到效果更好的东西
private void BuildPagesNew()
{
//CurrentPage is a public property. It's the ObservableCollection<FrameworkElement>
CurrentPage.Clear();
RichTextBlockOverflow lastOverflow;
lastOverflow = AddOnePage(null);
CurrentPage.Add(lastOverflow);
while(lastOverflow.HasOverflowContent)
{
lastOverflow = AddOnePage(lastOverflow);
}
}
private RichTextBlockOverflow AddOnePage(RichTextBlockOverflow lastOverflow)
{
bool isFirstPage = lastOverflow == null;
RichTextBlockOverflow rtbo = new RichTextBlockOverflow();
if (isFirstPage)
{
RichTextBlock pageOne = new RichTextBlock();
pageOne.Width = double.NaN;
pageOne.Height = double.NaN;
pageOne.FontSize = 16.00;
pageOne.MaxWidth = this.TextboxMaxWidth;
pageOne.MaxHeight = this.TextboxMaxHeight;
pageOne.HorizontalAlignment = HorizontalAlignment.Left;
pageOne.VerticalAlignment = VerticalAlignment.Top;
pageOne.IsDoubleTapEnabled = false;
pageOne.IsHitTestVisible = false;
pageOne.IsHoldingEnabled = false;
pageOne.IsTextSelectionEnabled = false;
pageOne.IsTapEnabled = false;
pageOne.SetValue(Helpers.Properties.HtmlProperty, CurrentBook.Pages[0].PageContent);
pageOne.SetBinding(RichTextBlock.MaxWidthProperty, new Binding
{
Source = TextboxMaxWidth,
Path = new PropertyPath("MaxWidth")
});
pageOne.SetBinding(RichTextBlock.MaxHeightProperty, new Binding
{
Source = TextboxMaxHeight,
Path = new PropertyPath("MaxHeight")
});
pageOne.Measure(new Size(this.TextboxMaxWidth, this.TextboxMaxHeight));
CurrentPage.Add(pageOne);
if (pageOne.HasOverflowContent)
{
pageOne.OverflowContentTarget = rtbo;
//set width and height here?
rtbo.Measure(new Size(this.TextboxMaxWidth, this.TextboxMaxHeight));
}
}
else
{
//set rtbo width and height here?
//Maybe set maxheight and maxwidth bindings too
if (lastOverflow.HasOverflowContent)
{
lastOverflow.OverflowContentTarget = rtbo;
lastOverflow.Measure(new Size(this.TextboxMaxWidth, this.TextboxMaxHeight));
rtbo.Measure((new Size(this.TextboxMaxWidth, this.TextboxMaxHeight)));
}
this.CurrentPage.Add(rtbo);
}
return rtbo;
}
private void BuildPagesNew()
{
//CurrentPage是公共属性。它是可观察集合
CurrentPage.Clear();
RichTextBlockOverflow-lastOverflow;
lastOverflow=AddOnePage(空);
CurrentPage.Add(lastOverflow);
while(lastOverflow.HasOverflowContent)
{
lastOverflow=AddOnePage(lastOverflow);
}
}
私有RichTextBlockOverflow AddOnePage(RichTextBlockOverflow lastOverflow)
{
bool isFirstPage=lastOverflow==null;
RichTextBlockOverflow rtbo=新的RichTextBlockOverflow();
如果(第一页)
{
RichTextBlock pageOne=新的RichTextBlock();
pageOne.Width=double.NaN;
pageOne.Height=double.NaN;
pageOne.FontSize=16.00;
pageOne.MaxWidth=this.TextboxMaxWidth;
pageOne.MaxHeight=此.TextboxMaxHeight;
pageOne.HorizontalAlignment=HorizontalAlignment.Left;
pageOne.VerticalAlignment=VerticalAlignment.Top;
pageOne.IsDoubleTapEnabled=false;
pageOne.ishitestvisible=false;
pageOne.IsHoldinEnabled=错误;
pageOne.IsTextSelectionEnabled=false;
pageOne.IsTapEnabled=错误;
pageOne.SetValue(Helpers.Properties.HtmlProperty,CurrentBook.Pages[0].PageContent);
pageOne.SetBinding(RichTextBlock.MaxWidthProperty,新绑定
{
Source=TextboxMaxWidth,
路径=新属性路径(“MaxWidth”)
});
pageOne.SetBinding(RichTextBlock.MaxHeightProperty,新绑定
{
Source=TextboxMaxHeight,
路径=新属性路径(“MaxHeight”)
});
pageOne.Measure(新尺寸(this.TextboxMaxWidth,this.TextboxMaxHeight));
当前页面。添加(第一页);
如果(pageOne.HasOverflowContent)
{
pageOne.OverflowContentTarget=rtbo;
//在这里设置宽度和高度?
rtbo.Measure(新尺寸(this.TextboxMaxWidth,this.TextboxMaxHeight));
}
}
其他的
{
//在此处设置rtbo宽度和高度?
//也可以设置maxheight和maxwidth绑定
if(lastOverflow.HasOverflowContent)
{
lastOverflow.OverflowContentTarget=rtbo;
Measure(新大小(this.TextboxMaxWidth,this.TextboxMaxHeight));
rtbo.Measure((新大小(this.TextboxMaxWidth,this.TextboxMaxHeight));
}
此.CurrentPage.Add(rtbo);
}
返回rtbo;
}