English 中文(简体)
如何为框架树立背景形象?
原标题:How to set background image for the frame?

I know in this article they set the background color using the steps followed. I was wondering if there was a way of using an image instead of a Color. I tried the following but it didn t work:

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

是否有任何人认为这样做是可能的? 还是局限于科罗人?

最佳回答

I decided to have a go and got it working. The only catch is, it s a bit of a workaround since there seems to be some strange behaviour with the ImageOpened event. Basically, the ImageOpened event of the Brush doesn t get called when you assign the background to the frame. Strangely, it does get called when you assign it to an element. So I just created a hidden button and assigned the brush to that (to force the ImageOpened event to fire). I then assigned it to the frame and it works for me. Seems like a bug, but the workaround below works for me.

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;
问题回答

使用

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

我工作。 页: 1

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

<CreateOption to None or 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;
 };




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签