【问题标题】:windows 8 xaml inline hyperlinkwindows 8 xaml 内联超链接
【发布时间】:2012-10-04 20:46:22
【问题描述】:

如何在 XAML 中的 Windows 应用商店应用中创建格式正确的超链接?我尝试创建一个内联超链接,并希望使用静态资源对其进行样式设置:

          <RichTextBlock Style="{StaticResource PageHeaderTextStyle}" Grid.ColumnSpan="2">
            <Paragraph>
                <Run>"A sentence with inline text "</Run>
                <InlineUIContainer>
                    <HyperlinkButton Background="Yellow">
                        my link
                    </HyperlinkButton>
                </InlineUIContainer>
                <Run>... some more text</Run>
            </Paragraph>
        </RichTextBlock>

我得到以下超链接与句子的其余部分不一致的地方:

【问题讨论】:

    标签: windows-8 microsoft-metro


    【解决方案1】:

    好吧,我试过这个没有用:

    <RichTextBlock FontSize="20">
        <Paragraph Foreground="White" FontFamily="Segoe UI Light">
            <Run>Now is the time for</Run>
            <InlineUIContainer>
                <HyperlinkButton Content="all good men">
                    <HyperlinkButton.Template>
                        <ControlTemplate>
                            <TextBlock Margin="5,0,5,0"  FontSize="20" FontFamily="Segoe UI Light"
                                        Text="{Binding Content, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" />
                        </ControlTemplate>
                    </HyperlinkButton.Template>
                </HyperlinkButton>
            </InlineUIContainer>
            <Run>to come to the aid of their country</Run>
        </Paragraph>
    </RichTextBlock>
    

    然后我尝试了这个:

    <RichTextBlock FontSize="20">
        <Paragraph Foreground="White" FontFamily="Segoe UI Light">
            <Run>Now is the time for</Run>
            <InlineUIContainer>
                <TextBlock Margin="5,0,5,0" Tapped="TextBlock_Tapped_1">
                    <Underline><Run Text="all good men" /></Underline>
                </TextBlock>
            </InlineUIContainer>
            <Run>to come to the aid of their country</Run>
        </Paragraph>
    </RichTextBlock>
    

    这就像一个魅力!

    我并不是假装实现您自己的超链接按钮并不需要更多的工作,而是这样想 - 您将 100% 控制其布局!而且它很容易继承它周围的字体样式!

    有意义吗?

    【讨论】:

    • 是的。我喜欢这个范式。谢谢。顺便说一句,run 标签有什么作用?
    • 运行标签允许您在文本块内执行绑定。它还允许您通过在单个文本块中运行多个运行来执行多个绑定。谷歌可以告诉你更多。
    • 我发现点击文本块会导致我的事件处理程序被调用两次。我读了一篇关于这个问题的帖子:stackoverflow.com/questions/3438806/…
    • 现在,剩下的就是看看如何让该死的东西可点击...... :(
    【解决方案2】:

    对于未来的读者来说,Windows 8.1 简化了这个任务,Windows 8.1 将 Hyperlink 元素添加到 Windows.UI.Xaml.Documents 命名空间中的 XAML 文本对象模型中,所以现在我们可以像这样简单地使用它:

    <RichTextBlock>
      <Paragraph FontSize="22">Please click on this <Hyperlink NavigateUri="http://www.link.com">link</Hyperlink>, thanks !</Paragraph>
    </RichTextBlock>
    

    它看起来像这样:

    【讨论】:

    • 那么如何更改此类超链接的默认已访问/未访问颜色?
    • Hyperlink 继承自 TextElement,所以您是否尝试更改 Foreground 属性?
    【解决方案3】:

    我也尝试解决这个问题,并提出以下建议:

    <RichTextBlock HorizontalAlignment="Left" VerticalAlignment="Top">
    <Paragraph xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" FontSize="12" FontFamily="Calibri" >
        <Run FontFamily="Calibri" FontSize="24">A sentence with inline text</Run>
        <InlineUIContainer>
            <HyperlinkButton FontSize="24" Background="Gray" Foreground="Blue" Template="{StaticResource HyperlinkButtonControlTemplate1}" BorderThickness="0" RenderTransformOrigin="0.5,0.5" Padding="0" FontFamily="Calibri">
                the link with g
            </HyperlinkButton>
        </InlineUIContainer>
        <Run FontFamily="Calibri" FontSize="24">and some more text</Run>
    </Paragraph>
    

    模板如下:

    <ControlTemplate x:Key="HyperlinkButtonControlTemplate1" TargetType="HyperlinkButton">
            <Grid>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="PointerOver">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                    Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkPointerOverForegroundThemeBrush}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Pressed">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                    Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkPressedForegroundThemeBrush}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                    Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource HyperlinkDisabledThemeBrush}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="FocusStates">
                        <VisualState x:Name="Focused">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="FocusVisualWhite"
                                    Storyboard.TargetProperty="Opacity"
                                    To="1"
                                    Duration="0" />
                                <DoubleAnimation Storyboard.TargetName="FocusVisualBlack"
                                    Storyboard.TargetProperty="Opacity"
                                    To="1"
                                    Duration="0" />
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Unfocused" />
                        <VisualState x:Name="PointerFocused" />
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
    
                <ContentPresenter x:Name="ContentPresenter"
                        Content="{TemplateBinding Content}"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        ContentTemplate="{TemplateBinding ContentTemplate}" 
                        RenderTransformOrigin="0.5,0.5" 
                        Margin="1,0" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Bottom" >
                    <ContentPresenter.RenderTransform>
                        <CompositeTransform TranslateY="8"/>
                    </ContentPresenter.RenderTransform>
                </ContentPresenter>
    
                <Rectangle x:Name="FocusVisualWhite"
                    IsHitTestVisible="False"
                    Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}"
                    StrokeEndLineCap="Square"
                    StrokeDashArray="1,1"
                    Opacity="0"
                    StrokeDashOffset="1.5" />
                <Rectangle x:Name="FocusVisualBlack"
                    IsHitTestVisible="False"
                    Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}"
                    StrokeEndLineCap="Square"
                    StrokeDashArray="1,1"
                    Opacity="0"
                    StrokeDashOffset="0.5" />
            </Grid>
        </ControlTemplate>
    

    唯一需要注意的是,&lt;CompositeTransform TranslateY="8"/&gt; 必须设置为字体大小的 1/3,在本例中为 8,因为字体大小为 24。并不理想,但它确实会产生所需的输出。

    更新: 或使用以下内容,这是从查看社交媒体 Windows 8 仪表板示例得出的 http://code.msdn.microsoft.com/wpapps/Social-Media-Dashboard-135436da

    <Paragraph xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" FontSize="12" FontFamily="Calibri" >
    <Run FontFamily="Calibri" FontSize="16">A sentence with inline text</Run>
    <Span>
    <InlineUIContainer>
            <HyperlinkButton FontSize="16" BorderThickness="0" Margin ="-10,-13" Foreground="Blue" FontFamily="Calibri">
            the link with g
        </HyperlinkButton>
    </InlineUIContainer>
    </Span>
    <Run FontFamily="Calibri" FontSize="16">and some more text</Run>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-07
      • 2018-08-30
      • 2013-09-18
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多