您希望构建一个小型应用程序,它允许浏览文件系统并显示多个文档。我要展示的一种文档是xps。 DocumentViewer表现不错。与框架结合使用,查看器可以处理内部链接(包含在xps文档中)。对于我的应用程序,我构建了一个自定义工具栏(zoom,page,fitsize ...),为每种文档都有一个工具栏。所以我需要删除documentViewer的工具栏。以下是代码。
<Style x:Key="{x:Type DocumentViewer}"
TargetType="{x:Type DocumentViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DocumentViewer}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Focusable="False">
<ScrollViewer
CanContentScroll="true"
HorizontalScrollBarVisibility="Auto"
x:Name="PART_ContentHost"
IsTabStop="true">
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
工作正常,但在激活xps中的链接后,再次显示DocumentViewer工具栏。如何避免?
答案 0 :(得分:1)
问题是导航服务首次点击链接后会创建新标准DocumentViewer
。即使您在XAML中使用从DocumentViewer
派生的组件,也会发生这种情况。
您可以通过手动重置导航容器的LayoutUpdated
事件
<强> XAML 强>
<Frame LayoutUpdated="OnFrameLayoutUpdated">
<Frame.Content>
<DocumentViewer ... />
</Frame.Content>
</Frame>
代码
private void OnFrameLayoutUpdated(object sender, EventArgs e)
{
var viewer = GetFirstChildByType<DocumentViewer>(this);
if (viewer == null) return;
viewer.Style = (Style) FindResource("DocumentViewerStyle");
}
private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
{
DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
if (child == null)
continue;
T castedProp = child as T;
if (castedProp != null)
return castedProp;
castedProp = GetFirstChildByType<T>(child);
if (castedProp != null)
return castedProp;
}
return null;
}