2012-10-04 104 views
8

如何在Windows應用商店應用中使用XAML創建格式正確的超鏈接?我試圖創建一個內嵌的超鏈接,並想用一個靜態資源的風格吧:windows 8 xaml內聯超鏈接

  <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> 

我得到以下其中的超級鏈接不與句子的其他部分對齊:

enter image description here

回答

8

好,我想這無濟於事:

<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> 

這就像一個魅力!

enter image description here

我不是假裝它是實現自己的超級鏈接按鈕,但認爲它是這樣的不是一點點更多的工作 - 你將有100%的控制它的佈局!它會很容易從它周圍的字體樣式繼承!

有意義嗎?

+0

肯定。我喜歡這個範例。謝謝。順便說一句,run標籤是做什麼的? – prostock

+0

運行標籤允許您在文本塊內執行綁定。它還允許您通過在單個文本塊中進行多次運行來執行多個綁定。谷歌可以告訴你更多。 –

+0

我發現點擊文本塊導致我的事件處理程序被調用兩次。我讀了一篇關於這個問題的文章:http://stackoverflow.com/questions/3438806/textbox-textchanged-event-firing-twice-on-windows-phone-7-emulator – prostock

0

我試圖解決這個問題,以及與以下想出了:

<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> 

唯一需要注意的是,<CompositeTransform TranslateY="8"/>絕設置爲字體大小的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> 

7

推導出未來的讀者只是添加了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> 

,它看起來像這樣:

enter image description here

+0

那麼改變這種超鏈接的默認訪問/未訪問的顏色? – Sevenate

+0

超鏈接從TextElement繼承,所以你嘗試更改Foreground屬性? –