The problem is that the navigation service creates a new standard DocumentViewer
after clicking on a link for the first time. This happens even when you use a component derived from DocumentViewer
in your XAML.
You can work around this issue by manually resetting the style in your navigation container's LayoutUpdated
event
XAML
<Frame LayoutUpdated="OnFrameLayoutUpdated">
<Frame.Content>
<DocumentViewer ... />
</Frame.Content>
</Frame>
Code behind
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;
}