2010-11-16 86 views
4

我是Silverlight和WP7的新手。我一直試圖通過使用HyperlinkBut​​ton並將其內容設置爲圖像來熱連接圖像。但是,這隻會讓我的圖像消失。WP7/Silverlight超鏈接圖片

重現:

  1. 創建一個新的Windows Phone應用全景。
  2. 在MainPage.xaml上用圖像替換矩形,將源設置爲ApplicationIcon.png。
  3. 然後,用HyperlinkBut​​ton將其包圍。
<HyperlinkButton NavigateUri="http://www.bing.com" TargetName="_blank"> 
    <Image Source="ApplicationIcon.png"/> 
</HyperlinkButton> 

我已經嘗試了很多屬性,沒有爲我工作。兩個項目都是相互依賴的。這在WP7中可能嗎?它是一個外部URI。我搜索文檔,發現沒有什麼幫助。

您的意見和建議表示讚賞。

回答

6

這是一個位,一個古怪的,你不能直接將影像作爲控制的內容。測試期間探討了該主題here

彼得托爾之前曾建議使用堆棧面板作爲超鏈接的內容。這在當時確實奏效,但由於某種原因,目前似乎並未奏效。

就這樣說,Richard Woo發現了一個圍繞它使用超鏈接背景屬性的工作。我證實了這一點還是工作原理如下:

<HyperlinkButton Height="310" HorizontalAlignment="Left" Margin="206,202,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="200" > 
     <HyperlinkButton.Background> 
      <ImageBrush ImageSource="SplashScreenImage.jpg"/> 
     </HyperlinkButton.Background> 
    </HyperlinkButton> 

這可能是值得提出這個作爲一個問題來看待爲上建議論壇或連接。

就超鏈接的替代品而言,Matt的選擇與圖像和手勢看起來可行。你也可以使用一個Button並重新設定其在Blend中的外觀。

+0

好的方法!謝謝。最後,我實際上只是圍繞整個StackPanel封裝了超鏈接。看起來有幾種方法可以解決這個問題,顯示這是我第一週使用XAML :) – nullable 2010-11-18 04:09:46

5

它看起來像你試圖使圖像啓動時點擊一個網頁。

啓動網頁的唯一方法是使用WebBrowserTask。如果我是你,我會在GestureListener(from the toolkit)中包裝圖像,並在點擊事件中啓動任務。

像這樣:

XAML:

<Image Source="images/appbar.favs.addto.rest.png" Stretch="None" > 
     <Controls:GestureService.GestureListener> 
      <Controls:GestureListener Tap="GestureListener_Tap" /> 
     </Controls:GestureService.GestureListener> 
    </Image> 

CS:

private void GestureListener_Tap(object sender, GestureEventArgs e) 
    { 
     var wbt = new WebBrowserTask(); 
     wbt.URL = "http://www.stackoverflow.com/"; 
     wbt.Show(); 
    } 
+0

謝謝,我會試試這個。我實際上鍊接到文檔和PDF文件。我不知道這是否是一個例外,因爲這些我們正在啓動我的設備超鏈接。 – nullable 2010-11-16 18:16:21

+0

工作正常。謝謝! – nullable 2010-11-16 18:51:21

0

您需要更改HyperlinkButton樣式以啓用其中的任何內容,而不僅僅是文本。這裏是一個樣本風格:

<Style x:Key="BrowserHyperlinkButtonStyle" TargetType="HyperlinkButton"> 
    <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}" /> 
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}" /> 
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}" /> 
    <Setter Property="FontWeight" Value="Normal" /> 
    <Setter Property="Background" Value="{StaticResource TransparentBrush}" /> 
    <Setter Property="BorderBrush" Value="{x:Null}" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="HorizontalContentAlignment" Value="Left" /> 
    <Setter Property="VerticalContentAlignment" Value="Top" /> 
    <Setter Property="Padding" Value="{StaticResource PhoneTouchTargetOverhang}" /> 
    <Setter Property="TargetName" Value="_blank" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="HyperlinkButton"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal" /> 
          <VisualState x:Name="MouseOver" /> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" 
                    Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame KeyTime="0" 
                   Value="{StaticResource PhoneSubtleBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" 
                    Storyboard.TargetProperty="Foreground"> 
             <DiscreteObjectKeyFrame KeyTime="0" 
                   Value="{StaticResource PhoneDisabledBrush}" /> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused" /> 
          <VisualState x:Name="Unfocused" /> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 
         <ContentControl Name="ContentElement" 
             Content="{TemplateBinding Content}" 
             ContentTemplate="{TemplateBinding ContentTemplate}" 
             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
             VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
             Margin="{TemplateBinding Padding}" /> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>