4

an app uses a background which is composed from a background color, a linar gradient and two radial gradient (looks better that it sounds :). Since this background is used on all pages I would like to define it once and than re-use it on all pages.

My first solution was to create a UserControl and apply the color and the gradient on it. I can then use this control on all pages as background.

This works fine but I wonder if there is a more elegant solution. Is it somehow possible to combine multiple brushes to one? I could then simply Apple "MyCombinedBrush" to the page directly instead of using an extra UserControl.

I found information that one could create an image and use it to create an ImageBrush. Unfortunately everything I found is limited to WPF and does not work on Windows Phone.

Is there any "elegant" way to solve this or is the UserControl the way to go?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225

1 Answers1

0

According to this - you can use ImageBrush on WP. (Although I've not tried this)

<TextBlock FontFamily="Verdana" FontSize="72">
  <TextBlock.Foreground>
    <ImageBrush ImageSource="forest.jpg"/>
  </TextBlock.Foreground>
</TextBlock>


EDIT:

Here is one solution I worked out - it has some disadvantages but works quite well and allows you to play quite well with many Brushes:

 Canvas canvasToBeBrush = new Canvas();
 canvasToBeBrush.Width = 300;
 canvasToBeBrush.Height = 300;
 Rectangle firstBrush = new Rectangle();
 firstBrush.Width = 200;
 firstBrush.Height = 200;
 firstBrush.Fill = new RadialGradientBrush(Colors.Blue, Colors.Brown);
 Rectangle secondBrush = new Rectangle();
 secondBrush.Width = 200;
 secondBrush.Height = 200;
 secondBrush.Opacity = 0.5;
 secondBrush.Fill = new SolidColorBrush(Colors.Orange);
 canvasToBeBrush.Children.Add(firstBrush);
 canvasToBeBrush.Children.Add(secondBrush);
 WriteableBitmap bitmapToBrush = new WriteableBitmap(canvasToBeBrush, null);
 ImageBrush myBrush = new ImageBrush();
 myBrush.ImageSource = bitmapToBrush;
 LayoutRoot.Background = myBrush;
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Getting in Image into an ImageBrush is not problem. But this is not the question. My Background is drawn using multiple Brusches (SolidBrush + Gradients). How can I create a combinded Brush? I found information saying that this would be possible by drawing the desired Pattern to an Image and using this as input for an ImageBrush. I do not know how to do this on WP. If there are any other solutions that do not use an ImageBrush this would be fine as well of course. – Andrei Herford Nov 12 '13 at 18:30
  • I've added one solution (still using ImageBrush) but works fine. – Romasz Nov 13 '13 at 08:43