C# 如何设置帧的背景图像?

C# 如何设置帧的背景图像?,c#,windows-phone-7,C#,Windows Phone 7,我知道他们是按照下面的步骤设置背景色的。我想知道是否有一种使用图像而不是颜色的方法。我尝试了以下方法,但无效: ImageBrush brush = new ImageBrush(); brush.ImageSource = new BitmapImage(new Uri("/MyApp;component/Images/Backgrounds/myimage.jpg")); Rootframe.Background = brush; 有人看到这是否可能吗?还是仅限于颜色?将CreateOp

我知道他们是按照下面的步骤设置背景色的。我想知道是否有一种使用图像而不是颜色的方法。我尝试了以下方法,但无效:

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

有人看到这是否可能吗?还是仅限于颜色?

CreateOption
设置为
None
BackgroundCreation
并在加载图像时等待:

 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;
 };

我决定试一试,让它发挥作用。唯一的问题是,这是一个解决办法,因为
ImageOpened
事件似乎有一些奇怪的行为。基本上,当您将背景指定给帧时,
画笔的
ImageOpened
事件不会被调用。奇怪的是,当您将它分配给元素时,它确实会被调用。所以我只是创建了一个隐藏按钮,并将画笔指定给它(强制触发
ImageOpened
事件)。然后我将其分配给框架,它对我有效。看起来像一个bug,但下面的解决方法对我很有效

ImageBrush brush = new ImageBrush();

brush.ImageSource = new BitmapImage(new Uri("/myImage.jpg", UriKind.Relative));
//hide the fake button and set the brush to be its background
button1.Visibility = System.Windows.Visibility.Collapsed;
button1.Background = brush;

//assign it to the frame (or using RootFrame in your case)
var frame = App.Current.RootVisual as PhoneApplicationFrame;
frame.Background = brush;
使用

对我有用。在
App.xaml

<ImageBrush x:Key="MainBackground" ImageSource="/resources/MainBackground.jpg" />


@Ky6opr我添加了这一点,但仍然没有运气。还有其他想法吗?我补充了这个,但过渡屏幕仍然是黑色的。我将看看是否可以从AppHub获得更多详细信息,看看我能找到什么。如果我得到答案,我会把它贴在这里。试着把图片加载到页面上的
image
控件中。问题可能在另一个地方…请在现有应用程序中使用图像而不是单独的程序集进行尝试。然后使用新的位图图像(新的Uri(“/Images/Backgrounds/myimage.jpg”)生成类型设置为
Content
。不幸的是,这也不起作用。这很奇怪,因为我可以设置系统颜色和自定义颜色。可能是其中的一件事,虽然被遗漏了。我试过你说的话,但仍然无法让它发挥作用。介意再发布一些代码吗。很抱歉。另外,我还是应该将图像设置为内容,还是应该返回资源?没有更多的代码可以发布,这几乎是来自一个新项目(在页面上添加了一个新按钮来伪造事件)。我把它放在我的页面的
Loaded
事件中。如果正在引用来自同一程序集的图像,则可以将其设置为内容。我没有您正在使用的工具包,因此我没有
RootFrame
。取而代之的是,我只使用了应用程序的主框架,我的背景在所有页面上都是一样的。我让它工作了,尽管出于某种原因,按钮从未工作过。相反,我放置了一个image控件,并在image_Opened事件上设置brush.ImageSource=image.Source。然后我设置了RootFrame.Background=brush,它可以完美地工作。感谢您的耐心和精彩的反馈+1.
<ImageBrush x:Key="MainBackground" ImageSource="/resources/MainBackground.jpg" />