简体   繁体   English

在C#中使用HyperlinkBut​​ton包装图像

[英]Wrap An Image With HyperlinkButton In C#

I can create a textual c# hyperlink... 我可以创建文本C#超链接...

HyperlinkButton sunday_title = new HyperlinkButton();
sunday_title.Content = "Sunday (" + sunday.ToUpper() + ")";
sunday_title.Click += sunday_link_Click;

But how do I create a graphical hyperlink? 但是,如何创建图形超链接? My first inclination was to use the image as a child, but that doesn't seem to be possible. 我的第一个倾向是将图像用作儿童,但这似乎是不可能的。

Here is the c# code to create the graphic... 这是创建图形的C#代码...

Image sunday_image = new Image();
sunday_image.Source = background_flcb;
Border sunday_border = new Border();
sunday_border.Child = sunday_image;

Ultimately, I want to make a whole grid a link (but I don't think that's possible). 最终,我想将整个grid作为链接(但我认为这是不可能的)。 At the moment, though, I would be happy enough just making the image a link. 不过,目前,只要将图像链接即可,我会很高兴。

Similar questions have been addressed, but most answers are with XAML. 已经解决了类似的问题 ,但是大多数答案都使用XAML。 I'm wanting to do it with c#. 我想用C#来做。

If it makes a difference, this is for a Windows 8 app (although the same logic should apply to a Windows Phone app). 如果有所不同,则适用于Windows 8应用程序(尽管相同的逻辑应适用于Windows Phone应用程序)。

In most case XAML can easily convert to C# code. 在大多数情况下,XAML可以轻松转换为C#代码。 For example, this XAML solution from Similar questions link above (simplified) : 例如,上面(简化)的来自类似问题的此XAML解决方案链接:

<HyperlinkButton>
    <HyperlinkButton.Background>
        <ImageBrush ImageSource="SplashScreenImage.jpg"/>
    </HyperlinkButton.Background>
</HyperlinkButton>

is equivalent to following C# code : 等效于以下C#代码:

var hyperlinkButton = new HyperlinkButton();
hyperlinkButton.Background = new ImageBrush{ImageSource = background_flcb};

Other suggested approach from the same link, is to simply use Image control and handle it's Click/Tap event to navigate to link destination : 来自同一链接的其他建议方法是,仅使用Image控件并处理其Click / Tap事件以导航到链接目标:

Image sunday_image = new Image();
sunday_image.Source = background_flcb;
Border sunday_border = new Border();
sunday_border.Child = sunday_image;
sunday_image.Tapped += sunday_image_tapped;

Anyway, I don't know whether those approaches will work in Windows 8 as well. 无论如何,我不知道这些方法在Windows 8中是否也适用。 Just give it a try. 试一试。 Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM