未知的超链接按钮 margin/padding 和 style/template
Unknown HyperlinkButton margin/padding with style/template
我正在尝试将 html 转换为 xaml 并使用 this code as a starting point。我的应用程序是 Windows Phone 8.1 通用应用程序。除了超链接外,这段代码工作得很好。看起来 HyperlingButton 有一些隐藏的边距或填充,我无法通过将其设置为 0 来修复它。
超链接标有红色边框:
我的风格在这里:
<Style TargetType="HyperlinkButton" x:Key="UnderlineHyperlinkButton">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="0,0,0,0" />
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HyperlinkButton">
<TextBlock Style="{StaticResource TextStylePostBody}">
<Underline>
<Run Text="{Binding Path=Content, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</Underline>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TextBlock 的样式:
<Style x:Key="TextStylePostBody" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="TextTrimming" Value="WordEllipsis"/>
<Setter Property="TextWrapping" Value="WrapWholeWords"/>
</Style>
我可以用边距中的硬编码值修复它 <Setter Property="Margin" Value="0,-9, 4,-4"/>
但它看起来不太好。
创建超链接的代码在这里:
private static Inline GenerateHyperLink(HtmlNode node)
{
Span s = new Span();
InlineUIContainer iui = new InlineUIContainer();
HyperlinkButton hb = new HyperlinkButton() { NavigateUri = new Uri(node.Attributes["href"].Value, UriKind.Absolute), Content = CleanText(node.InnerText) };
hb.Style = ( Style )Application.Current.Resources[ "UnderlineHyperlinkButton" ];
iui.Child = hb;
s.Inlines.Add(iui);
return s;
}
有什么我遗漏的吗?
更新:
使用此 XAMl 代码进行了快速测试:
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Foreground="Black" FontSize="18" FontWeight="Normal" FontFamily="Segroe UI">TextBlock</TextBlock>
<RichTextBlock>
<Paragraph>
<Run Foreground="Black" FontSize="18" FontWeight="Normal" FontFamily="Segroe UI">
<Run.Text>Run text</Run.Text>
</Run>
<InlineUIContainer>
<HyperlinkButton FontSize="18" FontFamily="Segroe UI" FontWeight="Normal" Margin="0" Padding="0,0,0,0">Link Text</HyperlinkButton>
</InlineUIContainer>
</Paragraph>
</RichTextBlock>
</StackPanel>
得到这个:
将 HyperlinkButton
的 Margin
设置为 -9(顶部):
不是其他文本或InlineUIContainer的问题,将HyperlinkButton
替换为TextBlock
修复边距:
由于对齐和格式问题,决定避免 InlineUIContainer
和 HyperlinkButton
。我已将其替换为 <Underline>
并附加了 Link
属性。 this article 中的更多信息。
编辑:不知道为什么,但我错过了运行时 API 有一个专门的 Hyperlink
class 可以很好地完成工作。
我正在尝试将 html 转换为 xaml 并使用 this code as a starting point。我的应用程序是 Windows Phone 8.1 通用应用程序。除了超链接外,这段代码工作得很好。看起来 HyperlingButton 有一些隐藏的边距或填充,我无法通过将其设置为 0 来修复它。
超链接标有红色边框:
我的风格在这里:
<Style TargetType="HyperlinkButton" x:Key="UnderlineHyperlinkButton">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="0,0,0,0" />
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HyperlinkButton">
<TextBlock Style="{StaticResource TextStylePostBody}">
<Underline>
<Run Text="{Binding Path=Content, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
</Underline>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TextBlock 的样式:
<Style x:Key="TextStylePostBody" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="TextTrimming" Value="WordEllipsis"/>
<Setter Property="TextWrapping" Value="WrapWholeWords"/>
</Style>
我可以用边距中的硬编码值修复它 <Setter Property="Margin" Value="0,-9, 4,-4"/>
但它看起来不太好。
创建超链接的代码在这里:
private static Inline GenerateHyperLink(HtmlNode node)
{
Span s = new Span();
InlineUIContainer iui = new InlineUIContainer();
HyperlinkButton hb = new HyperlinkButton() { NavigateUri = new Uri(node.Attributes["href"].Value, UriKind.Absolute), Content = CleanText(node.InnerText) };
hb.Style = ( Style )Application.Current.Resources[ "UnderlineHyperlinkButton" ];
iui.Child = hb;
s.Inlines.Add(iui);
return s;
}
有什么我遗漏的吗?
更新: 使用此 XAMl 代码进行了快速测试:
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Foreground="Black" FontSize="18" FontWeight="Normal" FontFamily="Segroe UI">TextBlock</TextBlock>
<RichTextBlock>
<Paragraph>
<Run Foreground="Black" FontSize="18" FontWeight="Normal" FontFamily="Segroe UI">
<Run.Text>Run text</Run.Text>
</Run>
<InlineUIContainer>
<HyperlinkButton FontSize="18" FontFamily="Segroe UI" FontWeight="Normal" Margin="0" Padding="0,0,0,0">Link Text</HyperlinkButton>
</InlineUIContainer>
</Paragraph>
</RichTextBlock>
</StackPanel>
得到这个:
将 HyperlinkButton
的 Margin
设置为 -9(顶部):
不是其他文本或InlineUIContainer的问题,将HyperlinkButton
替换为TextBlock
修复边距:
由于对齐和格式问题,决定避免 InlineUIContainer
和 HyperlinkButton
。我已将其替换为 <Underline>
并附加了 Link
属性。 this article 中的更多信息。
编辑:不知道为什么,但我错过了运行时 API 有一个专门的 Hyperlink
class 可以很好地完成工作。