WP7/Silverlight 超链接图片

4

我对Silverlight和WP7都很陌生。我一直试图通过使用HyperlinkButton并将其内容设置为Image来热链接图像。然而,这只会使我的Image消失。

复现步骤:

  1. 创建一个新的Windows Phone全景应用程序。
  2. 在MainPage.xaml中,将Rectangle替换为Image,并将源设置为ApplicationIcon.png。
  3. 然后,用HyperlinkButton将其包围。
<HyperlinkButton NavigateUri="http://www.bing.com" TargetName="_blank">
   <Image Source="ApplicationIcon.png"/>
</HyperlinkButton>

我尝试了很多属性,但没有任何效果。这两个项目是相互依存的。在WP7中是否可能实现这一点?它是一个外部URI。我已经搜索了文档,但没有找到有用的信息。

欢迎您提出评论和建议。

3个回答

6
这是一个奇怪的问题,因为您不能直接将图像放置为控件内容。该主题在测试期间已经讨论过,详情请参见此处
Peter Torr曾建议使用StackPanel作为超链接内容。当时确实可以使用,但现在由于某些原因似乎不再起作用。
尽管如此,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>

如果您认为这是一个问题,建议您在建议论坛或Connect上提出。

至于替代超链接的选择,Matt提供的图片和手势选项看起来可行。您也可以使用按钮并在Blend中重新设计其外观。


好的方法!谢谢。 最后我实际上只是将超链接包装在整个StackPanel周围。 看起来有几种方法可以解决这个问题,这表明这是我使用XAML的第一周 :) - nullable

5

看起来你想让图片在点击时启动一个网页。

唯一启动网页的方法是使用WebBrowserTask。如果我是你,我会在一个手势监听器中包装一个图像(从工具包中获取),并在触发tap事件时启动任务。

像这样:

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();
    }

谢谢,我会尝试这个。实际上,我正在链接文档和PDF文件。我不知道这是否是一个例外,因为在我的设备上,它们作为超链接启动得很好。 - nullable

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接