如何在WP7应用程序的代码隐藏文件中为silverlight中的按钮设置背景图像
我想知道如何在代码中而不是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码:
按预期工作。 BitmapImage 位于 System.Windows.dll 中,它应该已经在您的 WP7 项目中。
The code:
Works as expected. BitmapImage is in System.Windows.dll which should already be in your WP7 project.
我发现将 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