1

我正在我的应用程序中设置打印功能。为此,我有两个页面: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);
}

PageWidthDependencyProperty我在页面的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>

我有两个辅助方法,PrintPage打印OverflowPage服务调用它们:

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

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

然后的构造函数OverflowPage将参数的 (a RichTextBlock)OverflowContentTarget属性设置为OverflowPage's OverflowBlock

这一切都很好。但是,我似乎遇到了一个错误。系统似乎无法正确计算HasOverflowContent属性。正如你在这张图片中看到的,有明显的溢出内容(它被剪裁了),但HasOverflowContent总是错误的。

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

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

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

为文字墙道歉,但似乎是布局问题,我想我最好发布任何可能影响它的代码。

4

1 回答 1

1

HasOverflowContent不一定同步可用;您需要等待它可用。但仅仅听LayoutUpdated事件或awaiting下一个 UI 滴答声是不够的——你需要同时做这两个。来自相关问题的这个答案有代码可以做到这一点。

于 2017-12-23T01:13:50.583 回答