UIElement.OnRender(DrawingContext) 方法


在衍生類別中覆寫時,參與由配置系統導向的呈現作業。When overridden in a derived class, participates in rendering operations that are directed by the layout system. 這個項目的呈現指令不會在叫用此方法時直接使用,而是會加以保留,以便稍後由配置和繪圖做非同步使用。The rendering instructions for this element are not used directly when this method is invoked, and are instead preserved for later asynchronous use by layout and drawing.

 virtual void OnRender(System::Windows::Media::DrawingContext ^ drawingContext);
protected virtual void OnRender (System.Windows.Media.DrawingContext drawingContext);
abstract member OnRender : System.Windows.Media.DrawingContext -> unit
override this.OnRender : System.Windows.Media.DrawingContext -> unit
Protected Overridable Sub OnRender (drawingContext As DrawingContext)



特定項目的繪圖指令。The drawing instructions for a specific element. 此內容會提供給配置系統。This context is provided to the layout system.


下列程式碼範例顯示面板衍生類別的可能執行。The following code example shows a possible implementation for a panel derived class.

// Override the OnRender call to add a Background and Border to the OffSetPanel
protected override void OnRender(DrawingContext dc)
    SolidColorBrush mySolidColorBrush  = new SolidColorBrush();
    mySolidColorBrush.Color = Colors.LimeGreen;
    Pen myPen = new Pen(Brushes.Blue, 10);
    Rect myRect = new Rect(0, 0, 500, 500);
    dc.DrawRectangle(mySolidColorBrush, myPen, myRect);
' Override the OnRender call to add a Background and Border to the OffSetPanel
Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
    Dim mySolidColorBrush As New SolidColorBrush()
    mySolidColorBrush.Color = Colors.LimeGreen
    Dim myPen As New Pen(Brushes.Blue, 10)
    Dim myRect As New Rect(0, 0, 500, 500)
    dc.DrawRectangle(mySolidColorBrush, myPen, myRect)
End Sub


這個方法在類別中沒有預設的實值 UIElementThis method has no default implementation in the UIElement class.


OnRender(DrawingContext) 可以覆寫方法,以新增其他圖形元素 (先前未在邏輯樹狀結構中定義) 轉譯的元素,例如效果或裝飾項。The OnRender(DrawingContext) method can be overridden to add further graphical elements (not previously defined in a logical tree) to a rendered element, such as effects or adorners. DrawingContext物件會以引數的形式傳遞,以提供繪製圖形、文字、影像或影片的方法。A DrawingContext object is passed as an argument, which provides methods for drawing shapes, text, images or videos.