如何在WP7应用程序的代码隐藏文件中为silverlight中的按钮设置背景图像

发布于 2024-12-08 18:21:51 字数 325 浏览 0 评论 0原文

我想知道如何在代码中而不是在 xaml 中动态地将图像添加到按钮。有人建议按照以下方式继续

ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
AlphabetButton.Background = brush;

,但智能感知未检测到 BitmapImage,尽管我可以找到 BitConvertor 和 Bitmapcache。 如果这不是方法,我还能如何设置背景图像

I would like tho know how to add an image to a button dynamically in the code rather than in xaml. Someone had suggested to proceed the following way

ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
AlphabetButton.Background = brush;

but BitmapImage is not detected by the intellisense though i can find BitConvertor and Bitmapcache.
If this is not the method, how else would i set the background images

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潦草背影 2024-12-15 18:21:51

代码:

ImageBrush background = new ImageBrush();
background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"SplashScreenImage.jpg", UriKind.Relative));
Button1.Background = background;

按预期工作。 BitmapImage 位于 System.Windows.dll 中,它应该已经在您的 WP7 项目中。

The code:

ImageBrush background = new ImageBrush();
background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"SplashScreenImage.jpg", UriKind.Relative));
Button1.Background = background;

Works as expected. BitmapImage is in System.Windows.dll which should already be in your WP7 project.

厌倦 2024-12-15 18:21:51

我发现将 Image 控件放在要显示的按钮后面并让按钮具有透明背景会更容易、更简单。

这样就不需要为按钮创建单独的画笔,并且如果您想在图像上添加测试,也可以使其更具可扩展性。恕我直言

I find that it is much easier and simpler to just put an Image control behind the button that you want to display and leave the button with a transparent background.

This removes the need to create a separate brush for the button and also makes it more extensible if you want to add test over the image. IMHO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文