RichTextBlock HasOverflow 不正确
RichTextBlock HasOverflow is incorrect
我正在我的应用程序中设置打印功能。为此,我有两个页面:PrintPage
和 OverflowPage
。两者都继承自我的自定义摘要 Page
、PrintablePage
。以下是它们的 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="" /> = 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
是我在页面的 SizeChanged
事件处理程序中设置的 DependencyProperty
。
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=""
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
的 OverflowBlock
。
一切正常。但是,我似乎遇到了一个错误。系统似乎没有正确计算 HasOverflowContent
属性。正如您在这张图片中看到的,显然有溢出的内容(它正在被剪裁),但 HasOverflowContent
始终为 false。
(请注意,页面其余部分的内容已正确呈现,但出于隐私原因我已将其删除。)
这发生在 OverflowPage
,但我认为可以安全地假设 PrintPage
也会出现同样的问题。
我是否遗漏了影响布局的内容,或者这是系统中的错误?
对文字墙深表歉意,但似乎是布局问题,我想我最好 post 任何可能影响它的代码。
HasOverflowContent
不一定同步可用;您需要等待它可用。但是仅仅监听 LayoutUpdated
事件或 awaiting
下一个 UI 滴答是不够的——你需要两者都做。 来自相关问题的代码可以做到这一点。
我正在我的应用程序中设置打印功能。为此,我有两个页面:PrintPage
和 OverflowPage
。两者都继承自我的自定义摘要 Page
、PrintablePage
。以下是它们的 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="" /> = 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
是我在页面的 SizeChanged
事件处理程序中设置的 DependencyProperty
。
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=""
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
的 OverflowBlock
。
一切正常。但是,我似乎遇到了一个错误。系统似乎没有正确计算 HasOverflowContent
属性。正如您在这张图片中看到的,显然有溢出的内容(它正在被剪裁),但 HasOverflowContent
始终为 false。
这发生在 OverflowPage
,但我认为可以安全地假设 PrintPage
也会出现同样的问题。
我是否遗漏了影响布局的内容,或者这是系统中的错误?
对文字墙深表歉意,但似乎是布局问题,我想我最好 post 任何可能影响它的代码。
HasOverflowContent
不一定同步可用;您需要等待它可用。但是仅仅监听 LayoutUpdated
事件或 awaiting
下一个 UI 滴答是不够的——你需要两者都做。