一个应用程序使用的背景是由背景色,对角线渐变和两个径向渐变组成的(听起来更好:)。由于在所有页面上都使用了这种背景,因此我希望对其进行一次定义,而不是在所有页面上重复使用它。

我的第一个解决方案是创建一个UserControl并在其上应用颜色和渐变。然后,我可以在所有页面上将此控件用作背景。

这可以正常工作,但我想知道是否有更优雅的解决方案。是否可以将多个画笔组合成一个画笔?然后,我可以直接将Apple“MyCombinedBrush”直接添加到页面,而不使用额外的UserControl。

我发现可以创建一个图像并使用它来创建ImageBrush的信息。不幸的是,我发现的所有内容仅限于WPF,并且无法在Windows Phone上运行。

有什么“优雅”的方法可以解决此问题,或者UserControl是可行的方法吗?

最佳答案

根据this-您可以在WP上使用ImageBrush。 (尽管我没有尝试过)

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

编辑:

这是我解决的一种解决方案-它有一些缺点,但效果很好,并且可以让您很好地使用许多笔刷:
 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;

08-19 17:40