XamlCompositionBrushBase 클래스

정의

영역을 그리는 XAML 브러시를 만드는 데 사용되는 기본 클래스를 CompositionBrush제공합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

public ref class XamlCompositionBrushBase : Brush
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 262144)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class XamlCompositionBrushBase : Brush
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 262144)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class XamlCompositionBrushBase : Brush
Public Class XamlCompositionBrushBase
Inherits Brush
상속
Object IInspectable DependencyObject Brush XamlCompositionBrushBase
파생
특성

Windows 요구 사항

디바이스 패밀리
Windows 10 Creators Update (10.0.15063.0에서 도입되었습니다.)
API contract
Windows.Foundation.UniversalApiContract (v4.0에서 도입되었습니다.)

예제

이 예제에서는 Win2D 흐림 효과 및 CompositionBackdropBrush를 사용하여 브러시가 적용되는 UIElement 뒤에 있는 흐린 복사본을 그리는 사용자 지정 브러시에 대한 정의를 보여 줍니다.

public sealed class BackdropBlurBrush : XamlCompositionBrushBase
{
    public static readonly DependencyProperty BlurAmountProperty = DependencyProperty.Register(
        "BlurAmount",
        typeof(double),
        typeof(BackdropBlurBrush),
        new PropertyMetadata(0.0, new PropertyChangedCallback(OnBlurAmountChanged)
        )
    );

    public double BlurAmount
    {
        get { return (double)GetValue(BlurAmountProperty); }
        set { SetValue(BlurAmountProperty, value); }
    }

    private static void OnBlurAmountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var brush = (BackdropBlurBrush)d;
        // Unbox and set a new blur amount if the CompositionBrush exists.
        brush.CompositionBrush?.Properties.InsertScalar("Blur.BlurAmount", (float)(double)e.NewValue);            
    }

    public BackdropBlurBrush()
    {
    }

    protected override void OnConnected()
    {
        // Delay creating composition resources until they're required.
        if (CompositionBrush == null)
        {
            var backdrop = Window.Current.Compositor.CreateBackdropBrush();                

            // Use a Win2D blur affect applied to a CompositionBackdropBrush.
            var graphicsEffect = new GaussianBlurEffect
            {
                Name = "Blur",
                BlurAmount = (float)this.BlurAmount,
                Source = new CompositionEffectSourceParameter("backdrop")
            };

            var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
            var effectBrush = effectFactory.CreateBrush();

            effectBrush.SetSourceParameter("backdrop", backdrop);

            CompositionBrush = effectBrush;
        }
    }

    protected override void OnDisconnected()
    {
        // Dispose of composition resources when no longer in use.
        if (CompositionBrush != null)
        {
            CompositionBrush.Dispose();
            CompositionBrush = null;
        }
    }
}
Public NotInheritable Class BackdropBlurBrush
    Inherits XamlCompositionBrushBase

    Public Shared ReadOnly BlurAmountProperty As DependencyProperty = DependencyProperty.Register(
            "BlurAmount",
            GetType(Double),
            GetType(BackdropBlurBrush),
            New PropertyMetadata(0.0, New PropertyChangedCallback(AddressOf OnBlurAmountChanged)
            )
        )

    Public Property BlurAmount As Double
        Get
            Return DirectCast(GetValue(BlurAmountProperty), Double)
        End Get
        Set
            SetValue(BlurAmountProperty, Value)
        End Set
    End Property

    Private Shared Sub OnBlurAmountChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim brush = DirectCast(d, BackdropBlurBrush)
        ' Unbox And set a New blur amount if the CompositionBrush exists.
        brush.CompositionBrush?.Properties.InsertScalar("Blur.BlurAmount", Convert.ToSingle(DirectCast(e.NewValue, Double)))
    End Sub

    Protected Overrides Sub OnConnected()
        If Me.CompositionBrush Is Nothing Then

            Dim backdrop As CompositionBackdropBrush = Window.Current.Compositor.CreateBackdropBrush()

            ' Use a Win2D blur affect applied to a CompositionBackdropBrush.
            Dim graphicsEffect As GaussianBlurEffect = New GaussianBlurEffect()
            graphicsEffect.Name = "Blur"
            graphicsEffect.BlurAmount = Me.BlurAmount
            graphicsEffect.Source = New CompositionEffectSourceParameter("backdrop")

            Dim effectFactory As CompositionEffectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, New String() {"Blur.BlurAmount"})
            Dim effectBrush As CompositionEffectBrush = effectFactory.CreateBrush()

            effectBrush.SetSourceParameter("backdrop", backdrop)

            CompositionBrush = effectBrush
        End If
    End Sub

    Protected Overrides Sub OnDisconnected()
        ' Dispose of composition resources when no longer in use.
        If CompositionBrush IsNot Nothing Then
            CompositionBrush.Dispose()
            CompositionBrush = Nothing
        End If
    End Sub
End Class

아래 C++/WinRT 코드 예제의 경우 프로젝트에 Midl 파일(.idl) 파일을 추가해야 합니다.

// BackdropBlurBrush.idl
namespace MyApp
{
    [default_interface]
    runtimeclass BackdropBlurBrush : Windows.UI.Xaml.Media.XamlCompositionBrushBase
    {
        BackdropBlurBrush();
        static Windows.UI.Xaml.DependencyProperty BlurAmountProperty{ get; };
        Double BlurAmount;
    }
}
// pch.h
// You'll need to install the Microsoft Win2D NuGet package for this code example.
#include <winrt/Microsoft.Graphics.Canvas.Effects.h>
#include <winrt/Windows.Graphics.Effects.h>

// BackdropBlurBrush.h.
struct BackdropBlurBrush : BackdropBlurBrushT<BackdropBlurBrush>
{
    BackdropBlurBrush() = default;

    static Windows::UI::Xaml::DependencyProperty BlurAmountProperty() { return m_blurAmountProperty; }

    double BlurAmount()
    {
        return winrt::unbox_value<double>(GetValue(m_blurAmountProperty));
    }

    void BlurAmount(double value)
    {
        SetValue(m_blurAmountProperty, winrt::box_value(value));
    }

    void OnConnected();
    void OnDisconnected();

    static void OnBlurAmountChanged(Windows::UI::Xaml::DependencyObject const& d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs const& e);

private:
    static Windows::UI::Xaml::DependencyProperty m_blurAmountProperty;
};

// WindowBlurBrush.cpp.
Windows::UI::Xaml::DependencyProperty BackdropBlurBrush::m_blurAmountProperty =
    Windows::UI::Xaml::DependencyProperty::Register(
        L"BlurAmount",
        winrt::xaml_typename<double>(),
        winrt::xaml_typename<MyApp::BackdropBlurBrush>(),
        Windows::UI::Xaml::PropertyMetadata{ winrt::box_value(0.), Windows::UI::Xaml::PropertyChangedCallback{ &BackdropBlurBrush::OnBlurAmountChanged } }
);

void BackdropBlurBrush::OnBlurAmountChanged(Windows::UI::Xaml::DependencyObject const& d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs const& e)
{
    auto brush{ d.as<MyApp::BackdropBlurBrush>() };
    // Unbox and set a new blur amount if the CompositionBrush exists.
    if (brush.CompositionBrush() != nullptr)
    {
        brush.CompositionBrush().Properties().InsertScalar(L"Blur.BlurAmount", (float)winrt::unbox_value<double>(e.NewValue()));
    }
}

void BackdropBlurBrush::OnConnected()
{
    // Delay creating composition resources until they're required.
    if (!CompositionBrush())
    {
        auto backdrop{ Windows::UI::Xaml::Window::Current().Compositor().CreateBackdropBrush() };

        // Use a Win2D blur affect applied to a CompositionBackdropBrush.
        Microsoft::Graphics::Canvas::Effects::GaussianBlurEffect graphicsEffect{};
        graphicsEffect.Name(L"Blur");
        graphicsEffect.BlurAmount(this->BlurAmount());
        graphicsEffect.Source(Windows::UI::Composition::CompositionEffectSourceParameter(L"backdrop"));

        auto effectFactory{ Windows::UI::Xaml::Window::Current().Compositor().CreateEffectFactory(graphicsEffect, { L"Blur.BlurAmount" }) };
        auto effectBrush{ effectFactory.CreateBrush() };

        effectBrush.SetSourceParameter(L"backdrop", backdrop);

        CompositionBrush(effectBrush);
    }
}

void BackdropBlurBrush::OnDisconnected()
{
    // Dispose of composition resources when no longer in use.
    if (CompositionBrush())
    {
        CompositionBrush(nullptr);
    }
}
// WindowBlurBrush.h:
public ref class BackdropBlurBrush sealed :
    public Windows::UI::Xaml::Media::XamlCompositionBrushBase
{
public:
    BackdropBlurBrush();

    static property Windows::UI::Xaml::DependencyProperty^ BlurAmountProperty
    {
        Windows::UI::Xaml::DependencyProperty^ get() { return m_blurAmountProperty; }
    };

    property double BlurAmount
    {
        double get() 
        {
            return static_cast<double>(GetValue(BlurAmountProperty));
        }
        void set(double value) 
        {
            SetValue(BlurAmountProperty, value);
        }
    };

protected:
    virtual void OnConnected() override;
    virtual void OnDisconnected() override; 
    private:
    static Windows::UI::Xaml::DependencyProperty^ m_blurAmountProperty;
    static void OnBlurAmountChanged(Windows::UI::Xaml::DependencyObject^ d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ e);
};

// WindowBlurBrush.cpp:
DependencyProperty^ BackdropBlurBrush::m_blurAmountProperty = DependencyProperty::Register(
    "BlurAmount",
    Platform::String::typeid,
    BackdropBlurBrush::typeid,
    ref new PropertyMetadata(0.0, ref new PropertyChangedCallback(OnBlurAmountChanged))
);

BackdropBlurBrush::BackdropBlurBrush()
{
}

void BackdropBlurBrush::OnBlurAmountChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
{
    auto brush = static_cast<BackdropBlurBrush^>(d);
    // Unbox and set a new blur amount if the CompositionBrush exists
    if (brush->CompositionBrush != nullptr)
    {
        brush->CompositionBrush->Properties->InsertScalar("Blur.BlurAmount", (float)static_cast<double>(e->NewValue));
    }
}

void BackdropBlurBrush::OnConnected()
{
    // Delay creating composition resources until they're required
    if (CompositionBrush == nullptr)
    {
        auto backdrop = Window::Current->Compositor->CreateBackdropBrush();

        // Use a Win2D blur affect applied to a CompositionBackdropBrush
        auto graphicsEffect = ref new GaussianBlurEffect();
        graphicsEffect->Name = "Blur";
        graphicsEffect->BlurAmount = static_cast<float>(this->BlurAmount);
        graphicsEffect->Source = ref new CompositionEffectSourceParameter("backdrop");

        auto animatableProperties = ref new Platform::Collections::Vector<Platform::String^>();
        animatableProperties->Append("Blur.BlurAmount");

        auto effectFactory = Window::Current->Compositor->CreateEffectFactory(graphicsEffect, animatableProperties);
        auto effectBrush = effectFactory->CreateBrush();

        effectBrush->SetSourceParameter("backdrop", backdrop);

        CompositionBrush = effectBrush;
    }
}

void BackdropBlurBrush::OnDisconnected()
{
    // Dispose of composition resources when no longer in use
    if (CompositionBrush != nullptr)
    {
        delete CompositionBrush;
        CompositionBrush = nullptr;
    }
}

위의 브러시를 다른 XAML 브러시 형식처럼 사용하여 UIElements를 그릴 수 있습니다. 예를 들면 다음과 같습니다.

C++/WinRT의 경우 .#include "BackdropBlurBrush.h" MainPage.h

<Ellipse Width="100" Height="100">
    <Ellipse.Fill>
        <local:BackdropBlurBrush BlurAmount="10" />
    </Ellipse.Fill>
</Ellipse>

설명

XamlCompositionBrushBase를 사용하여 사용자 지정 브러시를 만들 수 있습니다.

예를 들어 CompositionEffectBrush를 사용하여 XAML UIElements에 효과를 적용하는 브러시를 만들거나, 조명이 켜질 때 XamlLight요소의 반사 속성을 제어하는 SceneLightingEffect 또는 더 복잡한 항목을 생성하기 위해 함께 연결된 전체 일련의 효과를 만드는 데 사용할 수 있습니다.

브러시를 만들 때 일반적으로 브러시를 사용할 때까지 관련 리소스 만들기 CompositionBrush 를 지연하는 것이 좋습니다. OnConnected 이 메서드는 브러시를 화면에서 처음 사용하여 요소를 그릴 때 호출되므로 필요한 경우에만 리소스를 안전하게 만들도록 재정 OnConnected 의할 수 있습니다. 즉, ResourceDictionary에서 브러시 인스턴스를 만든 다음 나중에 UI 정의의 다른 부분에서 해당 브러시 리소스를 참조하고 브러시가 실제로 사용 중일 때만 컴퍼지션 리소스를 만드는 비용을 지불할 수 있습니다.

더 이상 사용되지 않을 때 컴퍼지션 리소스를 삭제하는 것도 좋습니다. OnDisconnected 이 메서드는 브러시 인스턴스가 화면의 아무 곳에서도 더 이상 사용되지 않을 때 호출되므로 리소스를 안전하게 삭제하도록 재정 OnDisconnected 의할 수 있습니다. 나중에 연결이 끊긴 OnConnected 후 브러시를 다시 사용하면 다시 호출됩니다.

생성자

XamlCompositionBrushBase()

XamlCompositionBrushBase 파생 클래스에 대한 기본 클래스 초기화 동작을 제공합니다.

속성

CompositionBrush

이 XAML 브러시에서 CompositionBrush 사용되는 값을 가져오거나 설정합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase.CompositionBrush(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

Dispatcher

이 개체가 CoreDispatcher 연결된 개체를 가져옵니다. 코드 CoreDispatcher 가 비 UI 스레드에서 시작되는 경우에도 UI 스레드에 액세스할 DependencyObject 수 있는 기능을 나타냅니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.Dispatcher(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
FallbackColor

렌더링할 수 없는 경우 렌더링에 CompositionBrush 사용할 색입니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase.FallbackColor(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

FallbackColorProperty

FallbackColor 종속성 속성을 식별합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase.FallbackColorProperty(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

Opacity

Brush의 불투명도 수준을 가져오거나 설정합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.Brush.Opacity(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 Brush)
RelativeTransform

상대 좌표를 사용하여 브러시에 적용되는 변형을 가져오거나 설정합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.Brush.RelativeTransform(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 Brush)
Transform

브러시에 적용되는 변형을 가져오거나 설정합니다.

UWP용 동등한 WinUI 2 API: Microsoft.UI.Xaml.Media.Brush.Transform(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 Brush)

메서드

ClearValue(DependencyProperty)

종속성 속성의 로컬 값을 지웁니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.ClearValue(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

애니메이션이 활성화되지 않은 경우에 적용되는 종속성 속성에 대해 설정된 기본 값을 반환합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.GetAnimationBaseValue(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
GetValue(DependencyProperty)

에서 종속성 속성 DependencyObject의 현재 유효 값을 반환합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.GetValue(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
OnConnected()

브러시를 화면에 처음 사용하여 요소를 그릴 때 호출됩니다.

파생 클래스에서 구현되는 경우 인스턴스를 CompositionBrush 만들고 속성을 설정 CompositionBrush 하여 프레임워크에 제공할 수 있습니다.

OnDisconnected 는 브러시가 더 이상 요소를 그리는 데 사용되지 않을 때 호출됩니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase.OnConnected(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

OnDisconnected()

브러시가 더 이상 요소를 그리는 데 사용되지 않을 때 호출됩니다.

파생 클래스에서 구현되는 경우 퇴비 브러시 및 기타 컴퍼지션 리소스를 안전하게 삭제할 수 있습니다.

OnConnected 는 나중에 브러시를 사용하여 연결이 끊긴 후 요소를 그리는 경우 다시 호출됩니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.XamlCompositionBrushBase.OnDisconnected(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

PopulatePropertyInfo(String, AnimationPropertyInfo)

애니메이션 효과를 적용할 수 있는 속성을 정의합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.Brush.PopulatePropertyInfo(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 Brush)
PopulatePropertyInfoOverride(String, AnimationPropertyInfo)

파생 클래스에서 재정의되는 경우 애니메이션 효과를 적용할 수 있는 속성을 정의합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.Media.Brush.PopulatePropertyInfoOverride(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 Brush)
ReadLocalValue(DependencyProperty)

로컬 값이 설정된 경우 종속성 속성의 로컬 값을 반환합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.ReadLocalValue(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

DependencyObject 인스턴스의 특정 DependencyProperty 변경 내용을 수신 대기하기 위한 알림 함수를 등록합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.RegisterPropertyChangedCallback(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
SetValue(DependencyProperty, Object)

에 종속성 속성 DependencyObject의 로컬 값을 설정합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.SetValue(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

를 호출 RegisterPropertyChangedCallback하여 이전에 등록한 변경 알림을 취소합니다.

UWP에 해당하는 WinUI 2 API: Microsoft.UI.Xaml.DependencyObject.UnregisterPropertyChangedCallback(Windows 앱 SDK WinUI의 경우 Windows 앱 SDK 네임스페이스 참조).

(다음에서 상속됨 DependencyObject)

적용 대상

추가 정보