2010-11-16 88 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>