【问题标题】:WP7/Silverlight Hyperlink ImageWP7/Silverlight 超链接图片
【发布时间】:2011-05-10 23:13:40
【问题描述】:

我是 Silverlight 和 WP7 的新手。我一直在尝试通过使用 HyperlinkBut​​ton 并将其内容设置为图像来热链接图像。但是,这只会使我的图像消失。

复制:

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

我已经尝试了许多属性,但没有一个对我有用。这两个项目相互依赖地工作。这在WP7中可能吗?它是一个外部 URI。我搜索了文档,但没有找到任何帮助。

感谢您的 cmets 和建议。

【问题讨论】:

    标签: silverlight image windows-phone-7 hyperlink


    【解决方案1】:

    这有点奇怪,因为您不能直接将 Image 作为控件的内容。该主题在测试期间被探索here

    Peter Torr 之前曾建议使用堆栈面板作为超链接的内容。这在当时确实有效,但由于某种原因目前似乎无效。

    话虽如此,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 中的外观。

    【讨论】:

    • 不错的方法!谢谢你。最后,我实际上只是将超链接包裹在整个 StackPanel 周围。看起来有几种方法可以解决这个问题,表明这是我使用 XAML 的第一周 :)
    【解决方案2】:

    您似乎正试图让图像在被点击时启动网页。

    启动网页的唯一方法是使用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();
        }
    

    【讨论】:

    • 谢谢,我试试这个。我实际上链接到 doc 和 pdf 文件。我不知道这是否是一个例外,因为这些我们在我的设备上作为超链接正常启动。
    【解决方案3】:

    您需要更改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>
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 2015-07-13
      • 2010-10-09
      • 2019-11-15
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      相关资源
      最近更新 更多