확장성 - .NET WPFExtensibility - .NET WPF

사용자 지정 요소 렌더링Custom Element Rendering

렌더러를 완벽하게 제어하려면 ElementRenderers 속성을 사용하여 기본 렌더러를 추가, 제거 또는 재정의하면 됩니다.For full control of the renderer you can use the ElementRenderers property to add, remove, or override default renderers.

다음 예제에서는 사용자 지정 "type": "Rating" 요소를 정의하고 렌더링하는 방법을 보여줍니다.The following example shows how you could define a custom "type": "Rating" element and render it.

// Register the new type with the JSON parser
AdaptiveTypedElementConverter.RegisterTypedElement<MyCustomRating>();

// Add the new type to the element renderer registry
renderer.ElementRenderers.Set<MyCustomRating>(MyCustomRating.Render);

// Define a custom Rating element type
public class MyCustomRating : AdaptiveElement
{
    public MyCustomRating() { Type = "Rating"; }

    public override string Type { get; set; }

    public AdaptiveTextSize Size { get; set; }

    public AdaptiveTextColor Color { get; set; }

    public static FrameworkElement Render(MyCustomRating rating, AdaptiveRenderContext context)
    {
        var textBlock = new AdaptiveTextBlock
        {
            Size = rating.Size,
            Color = rating.Color
        };
        for (int i = 0; i < rating.Rating; i++)
        {
            textBlock.Text += "\u2605";
        }
        textBlock.Text += $" ({rating.Rating})";
        return context.Render(textBlock);
    }
}