如何设置框架的背景图像?

时间:2012-01-12 15:07:44

标签: c# windows-phone-7

我知道在this article他们使用所遵循的步骤设置背景颜色。我想知道是否有使用图像而不是颜色的方法。我尝试了以下但是没有用:

ImageBrush brush = new ImageBrush();
brush.ImageSource = 
new BitmapImage(new Uri("/MyApp;component/Images/Backgrounds/myimage.jpg"));
Rootframe.Background = brush;

有没有人看过这是否可能?还是仅限于颜色?

3 个答案:

答案 0 :(得分:5)

我决定去吧,让它运转起来。唯一的问题是,这是一个解决方法,因为ImageOpened事件似乎有一些奇怪的行为。基本上,当您为帧指定背景时,ImageOpened的{​​{1}}事件不会被调用。奇怪的是,当你将它分配给一个元素时,它会被调用。所以我只是创建了一个隐藏按钮并将画笔指定给它(强制Brush事件触发)。然后我将它分配到框架,它适用于我。看起来像一个错误,但下面的解决方法适合我。

ImageOpened

答案 1 :(得分:4)

使用

RootFrame.Background = App.Current.Resources["MainBackground"] as ImageBrush;

适合我。您需要在App.xaml

的资源字典中添加以下内容
<ImageBrush x:Key="MainBackground" ImageSource="/resources/MainBackground.jpg" />

答案 2 :(得分:1)

CreateOption设置为NoneBackgroundCreation并等待图片加载:

 BitmapImage image = new BitmapImage(new Uri("/MyApp;component/Images/Backgrounds/myimage.jpg"))
 {
      CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.None
 };
 image.ImageOpened += (s, e) =>
 {
      brush.ImageSource = image;
      App.RootFrame.Background = brush;
 };