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

alf*_*fah 2 button background-image silverlight-4.0 windows-phone-7

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

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

但是,虽然我可以找到BitConvertor和Bitmapcache,但智能感知不会检测到BitmapImage.如果这不是方法,我将如何设置背景图像

小智 8

代码:

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

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