System.Windows.Media 命名空间


.NET Framework 类库
VisualBrush 类

更新:2007 年 11 月

使用 Visual 绘制区域。

命名空间:  System.Windows.Media
程序集:  PresentationCore(在 PresentationCore.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation

语法

Visual Basic(声明)
Public NotInheritable Class VisualBrush _
    Inherits TileBrush
Visual Basic (用法)
Dim instance As VisualBrush
C#
public sealed class VisualBrush : TileBrush
Visual C++
public ref class VisualBrush sealed : public TileBrush
J#
public final class VisualBrush extends TileBrush
JScript
public final class VisualBrush extends TileBrush
XAML 对象元素用法
<VisualBrush .../>
备注

指定 VisualBrushVisual 内容有两种方法。

  • 创建一个新 Visual,并使用它来设置 VisualBrushVisual 属性。

  • 使用现有 Visual,它将创建目标 Visual 的重复图像。然后可以使用 VisualBrush 来创建一些有趣的效果,例如反射和放大。

如果您为 VisualBrush 定义一个新的 Visual,且该 Visual 是一个 UIElement(如面板或控件),则当 AutoLayoutContent 属性设置为 true 时,布局系统将在 UIElement 及其子元素上运行。但是,此根 UIElement 实际上是与系统的其余部分隔离的;样式、演示图板和外部布局无法充满此边界。因此,应该显式指定该根 UIElement 的大小,因为它的唯一父级是 VisualBrush,所以它本身无法自动调整大小以适合要绘制的区域。有关 Windows Presentation Foundation (WPF) 中布局的更多信息,请参见布局系统

如果 BitmapEffect 在画笔的父链中,则对 VisualBrush 的可视化树的更新将不会传播。您可以强制对象场景的更新不受该效果的影响,从而避开此限制。可以调用 InvalidateVisual 或包含一个动画,从而强制场景更新。

Freezable 功能:由于它继承自 Freezable 类,因此 VisualBrush 类提供了几个特殊功能:VisualBrush 对象可按资源中的方式进行声明并在多个对象之间共享。有关 Freezable 对象提供的不同功能的更多信息,请参见Freezable 对象概述

说明:

VisualBrushVisual 属性设置为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing 之外的任何值时,都无法将它设置为只读(冻结)。

示例

本示例演示如何使用 VisualBrush 类用 Visual 绘制一个区域。

在下面的示例中,使用了几个控件和一个面板作为一个矩形的背景。

XAML
<Rectangle Width="150" Height="150" Stroke="Black" Margin="5,0,5,0">
  <Rectangle.Fill>
    <VisualBrush>
      <VisualBrush.Visual>
        <StackPanel Background="White">
          <Rectangle Width="25" Height="25" Fill="Red" Margin="2" />
          <TextBlock FontSize="10pt" Margin="2">Hello, World!</TextBlock>
          <Button Margin="2">A Button</Button>
        </StackPanel>
      </VisualBrush.Visual>
    </VisualBrush>
  </Rectangle.Fill>
</Rectangle>
C#
VisualBrush myVisualBrush = new VisualBrush();

// Create the visual brush's contents.
StackPanel myStackPanel = new StackPanel();
myStackPanel.Background = Brushes.White;

Rectangle redRectangle = new Rectangle();
redRectangle.Width = 25;
redRectangle.Height =25; 
redRectangle.Fill = Brushes.Red;
redRectangle.Margin = new Thickness(2);
myStackPanel.Children.Add(redRectangle);

TextBlock someText = new TextBlock();
FontSizeConverter myFontSizeConverter = new FontSizeConverter();
someText.FontSize = (double)myFontSizeConverter.ConvertFrom("10pt");
someText.Text = "Hello, World!";
someText.Margin = new Thickness(2);
myStackPanel.Children.Add(someText);

Button aButton = new Button();
aButton.Content = "A Button";
aButton.Margin = new Thickness(2);
myStackPanel.Children.Add(aButton);

// Use myStackPanel as myVisualBrush's content.
myVisualBrush.Visual = myStackPanel;

// Create a rectangle to paint.
Rectangle myRectangle = new Rectangle();
myRectangle.Width = 150;
myRectangle.Height = 150;
myRectangle.Stroke = Brushes.Black;
myRectangle.Margin = new Thickness(5,0,5,0);

// Use myVisualBrush to paint myRectangle.
myRectangle.Fill = myVisualBrush;

有关 VisualBrush 的更多信息以及其他示例,请参见使用图像、绘图和 Visual 进行绘制概述。

此代码示例摘自为 VisualBrush 类提供的一个更大的示例。有关完整示例,请参见 VisualBrush 示例

更多代码

如何:创建反射本示例演示如何使用 VisualBrush 创建反射。由于 VisualBrush 可以显示现有可视项目,您可以使用此功能生成有趣的可视效果,例如反射和放大。
如何:使用视频绘制区域此示例演示如何绘制带有媒体的区域。若要绘制带有媒体的区域,一种方法就是结合使用 MediaElementVisualBrush。使用 MediaElement 加载和播放媒体,再使用该元素来设置 VisualBrushVisual 属性,然后可以使用 VisualBrush 来绘制带有已加载媒体的区域。
继承层次结构

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows..::.Freezable
        System.Windows.Media.Animation..::.Animatable
          System.Windows.Media..::.Brush
            System.Windows.Media..::.TileBrush
              System.Windows.Media..::.VisualBrush
线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.5、3.0
另请参见

参考

其他资源

标记 :


Page view tracker