【问题标题】:TextBox Alignment When Printing From UWP从 UWP 打印时的文本框对齐
【发布时间】:2016-07-20 16:46:04
【问题描述】:

我正在尝试从 UWP 应用打印一页文本,但我遇到了对齐问题。首先,我创建了一个包含以下 xaml 的页面:

<Grid Background="White">
    <TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>

注意 TextBlock 如何在网格中居中对齐。

然后,我使用各种 PrintDocument 事件处理程序打印它;这是打印预览它的代码(简化为忽略边距和不可打印区域):

Page printPage;

private void OnPrintDocumentPaginate(object sender, PaginateEventArgs e)
{
    // Construct an instance of the page to print, and tell Windows that there is only 1 page
    this.printPage = new PrintPage();

    printDocument.SetPreviewPageCount(1, PreviewPageCountType.Final);
}

private void OnPrintDocumentGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
    // Give Windows a reference to the page to print for preview
    this.printDocument.SetPreviewPage(e.PageNumber, this.printPage);
}

这就是它的打印方式:

请注意,TextBlock 没有居中。

但是,如果我将 TextBlock 包装在边框中,如下所示:

<Grid Background="White">
    <Border HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black"/>
    </Border>
</Grid>

那就好了:

任何人都可以看到为什么使用原始 xaml 时 TextBlock 不居中吗?

【问题讨论】:

  • 将垂直对齐和水平对齐也设置为网格的中心。
  • 这行得通,但是如果我希望其他文本块在其他位置对齐,例如在右下角,我会遇到问题,所以我认为网格需要填充页面。我发现我可以使用 TextBox 而不是 TextBlock,BorderThickess 为 0,所以它只是打印页面上的文本。

标签: xaml win-universal-app


【解决方案1】:

我从 Windows 8.1 升级到 UWP 项目时遇到了同样的问题。

在 UWP 中打印时,TextBlock.VerticalAlignmentTextBlock.Horizo​​ntalAlignment 属性无法按预期工作。

但是,如果您使用 Run 元素而不是 TextBlock.Text 属性,它们似乎可以按预期工作:

<TextBlock FontSize="36" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Run Text="Welcome Printed World!" />
</TextBlock>

您也可以使用 TextBlock.Margin 进行垂直对齐,使用 TextBlock.TextAlignment 进行水平对齐。

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2022-11-11
    • 2011-01-29
    相关资源
    最近更新 更多