RichTextBlock HasOverflow不正确

时间:2017-12-21 16:35:17

标签: c# xaml printing uwp richtextblock

我正在我的应用中设置打印功能。为此,我有两个页面:PrintPageOverflowPage。两者都继承自我的自定义摘要PagePrintablePage。以下是它们的XAML布局:

PrintPage.xaml

<RelativePanel>

    <RelativePanel x:Name="Header"
                   BorderBrush="Gray"
                   BorderThickness="0 0 0 1"
                   Margin="48 48 48 12"
                   Padding="0 0 0 6"
                   RelativePanel.AlignLeftWithPanel="True"
                   RelativePanel.AlignRightWithPanel="True"
                   RelativePanel.AlignTopWithPanel="True">

        <TextBlock x:Name="RouteTextBlock"
                   Style="{ThemeResource TitleTextBlockStyle}"
                   Text="{x:Bind Route, Mode=OneWay}" />
        <TextBlock x:Name="StaffTextBlock"
                   Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
                   Margin="0 6 0 0"
                   RelativePanel.Below="RouteTextBlock"
                   Text="{x:Bind Staff, Mode=OneWay}"
                   TextWrapping="Wrap" />

        <TextBlock x:Name="Legend"
                   FontSize="14"
                   Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
                   Margin="0 12 0 24"
                   RelativePanel.Below="StaffTextBlock">

            <Run FontFamily="/Assets/Fonts/Kimble.ttf#Kimble-UWP"
                 FontSize="16"
                 Text="&#xf106;" /> = Parent must be present for pickup

        </TextBlock>

        <Grid x:Name="DataHeader"
              RelativePanel.AlignLeftWithPanel="True"
              RelativePanel.AlignRightWithPanel="True"
              RelativePanel.Below="Legend">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="24" />
                <ColumnDefinition Width="48" />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition Width="60" />
            </Grid.ColumnDefinitions>

            <TextBlock x:Name="NameHeader"
                       FontWeight="SemiBold"
                       Grid.Column="2"
                       Grid.Row="2"
                       Text="Name" />
            <TextBlock x:Name="AddressHeader"
                       FontWeight="SemiBold"
                       Grid.Column="3"
                       Grid.Row="2"
                       Text="Address" />
            <TextBlock x:Name="AgeHeader"
                       FontWeight="SemiBold"
                       Grid.Column="4"
                       Grid.Row="2"
                       HorizontalTextAlignment="Right"
                       Text="Age" />
        </Grid>

    </RelativePanel>

    <RichTextBlock x:Name="MainContentTextBlock"
                   HorizontalAlignment="Stretch"
                   RelativePanel.Above="InstructionsTextBlock"
                   RelativePanel.AlignLeftWithPanel="True"
                   RelativePanel.AlignRightWithPanel="True"
                   RelativePanel.Below="Header" />

    <TextBlock x:Name="InstructionsTextBlock"
               Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
               Margin="0 0 0 24"
               RelativePanel.AlignHorizontalCenterWithPanel="True"
               RelativePanel.AlignBottomWithPanel="True"
               Text="Please fill out this form and return it at the end of the night" />

</RelativePanel>

OverflowPage.xaml

<RelativePanel Margin="0 0 0 12">

    <Grid x:Name="Header"
          BorderBrush="Gray"
          BorderThickness="0 0 0 1"
          Margin="48 48 48 12"
          Padding="0 0 0 6"
          RelativePanel.AlignLeftWithPanel="True"
          RelativePanel.AlignRightWithPanel="True"
          RelativePanel.AlignTopWithPanel="True">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="24" />
            <ColumnDefinition Width="48" />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition Width="60" />
        </Grid.ColumnDefinitions>

        <TextBlock x:Name="NameHeader"
                   FontWeight="SemiBold"
                   Grid.Column="2"
                   Grid.Row="2"
                   Text="Name" />
        <TextBlock x:Name="AddressHeader"
                   FontWeight="SemiBold"
                   Grid.Column="3"
                   Grid.Row="2"
                   Text="Address" />
        <TextBlock x:Name="AgeHeader"
                   FontWeight="SemiBold"
                   Grid.Column="4"
                   Grid.Row="2"
                   HorizontalTextAlignment="Right"
                   Text="Age" />
    </Grid>

    <RichTextBlockOverflow x:Name="OverflowBlock"
                           RelativePanel.AlignLeftWithPanel="True"
                           RelativePanel.AlignRightWithPanel="True"
                           RelativePanel.Below="Header" />

</RelativePanel>

PrintPage的构造函数中,我呈现了一个项目列表:

var pageWidthBinding = new Binding
{
    FallbackValue = 0,
    TargetNullValue = 0,
    Source = this,
    Path = new PropertyPath("PageWidth"),
    Mode = BindingMode.OneWay
};

foreach (var child in Children)
{
    var uiContainer = new InlineUIContainer();
    var contentPresenter = new ContentPresenter
    {
        Content = child,
        ContentTemplate = ChildItemTemplate
    };
    contentPresenter.SetBinding(WidthProperty, pageWidthBinding);
    uiContainer.Child = contentPresenter;
    var paragraph = new Paragraph { LineHeight = 18, Margin = new Thickness(0) };
    paragraph.Inlines.Add(uiContainer);
    MainContentTextBlock.Blocks.Add(paragraph);
}

PageWidth是我在页面的DependencyProperty事件处理程序中设置的SizeChanged

ChildItemTemplate

<DataTemplate x:Name="ChildItemTemplate"
              x:DataType="models:Child">
    <Grid Height="18"
          Margin="48 12">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="24" />
            <ColumnDefinition Width="48" />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition Width="60" />
        </Grid.ColumnDefinitions>

        <FontIcon x:Name="ParentPresentIcon"
                  x:Load="{x:Bind ParentMustBePresent}"
                  FontFamily="/Assets/Fonts/Kimble.ttf#Kimble-UWP"
                  FontSize="16"
                  Foreground="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
                  Glyph="&#xf106;"
                  Grid.Column="0"
                  HorizontalAlignment="Center" />
        <Border x:Name="HereCheckBox"
                BorderBrush="{ThemeResource ApplicationSecondaryForegroundThemeBrush}"
                BorderThickness="1"
                CornerRadius="2"
                Grid.Column="1"
                Height="18"
                HorizontalAlignment="Center"
                Width="18" />
        <TextBlock Grid.Column="2"
                   Text="{x:Bind FullName}" />
        <TextBlock Grid.Column="3"
                   Text="{x:Bind Address}" />
        <TextBlock Grid.Column="4"
                   HorizontalTextAlignment="Right"
                   Text="{x:Bind Age}" />
    </Grid>
</DataTemplate>

我在PrintPageOverflowPage上有两个帮助方法,打印服务调用这些方法:

public override PrintablePage GetOverflowContent() =>
    new OverflowPage(MainContentTextBlock);

public override bool HasOverflow() => 
    MainContentTextBlock.HasOverflowContent;

OverflowPage的构造函数然后将参数(RichTextBlockOverflowContentTarget属性设置为OverflowPage的{​​{1}}。

一切正常。但是,我似乎遇到了一个错误。系统似乎没有正确计算OverflowBlock属性。正如您在此图片中看到的那样,显然存在溢出内容(它被裁剪),但HasOverflowContent始终为假。

Paper (请注意,内容在页面的其余部分正确呈现,但出于隐私原因,我已将其删除。)

这发生在HasOverflowContent,但我认为可以安全地假设同样的问题也会出现在OverflowPage上。

我是否遗漏了影响我布局的内容,或者这是系统中的错误?

对于文本墙的道歉,但看似布局问题,我想我最好发布任何可能影响它的代码。

1 个答案:

答案 0 :(得分:1)

HasOverflowContent不一定同步可用;你需要等待它变得可用。但是,只需听取LayoutUpdated事件或awaiting以获取下一个用户界限,就不够了 - 您需要同时执行这两项操作。来自相关问题的This answer有代码可以执行此操作。