XamlCompositionBrushBase 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一個基底類別,用於建立使用 CompositionBrush 繪製區域的 XAML 筆刷。
public ref class XamlCompositionBrushBase : Brush
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [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(Microsoft.UI.Xaml.WinUIContract), 65536)]
[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
- 繼承
- 衍生
- 屬性
範例
這個範例展示了一個自訂畫筆的定義,該筆刷會用 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;
}
}
}
以下的 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 筆刷類型一樣用於繪製 UIElement,例如:
對於 C++/WinRT,也要加上 #include "BackdropBlurBrush.h"MainPage.h。
<Ellipse Width="100" Height="100">
<Ellipse.Fill>
<local:BackdropBlurBrush BlurAmount="10" />
</Ellipse.Fill>
</Ellipse>
備註
你可以使用 XamlCompositionBrushBase 來建立自訂筆刷。
例如,它可以用來建立一個刷子,使用 CompositionEffectBrush 為 XAML UIElement 套用效果,或用 SceneLightingEffect 控制被 XamlLight 照亮時元素的反射特性,或是將一連串效果串連起來,產生更複雜的效果。
在製作畫筆時,通常建議等到畫筆被使用時再開始製作 CompositionBrush 及相關資源。 OnConnected 方法是在螢幕上首次使用筆刷來繪製元素時呼叫,因此你可以覆寫 OnConnected ,只在需要時安全建立資源。 這表示你可以在 ResourceDictionary 中建立一個筆刷實例,然後再從 UI 定義的其他部分引用該筆刷資源,只有在筆刷實際使用時才支付創建合成資源的成本。
當作文資源不再使用時,處理掉它們也是個好習慣。 當畫面上某個刷子實例不再使用時,會呼叫 OnDisconnected 方法,這樣你可以覆寫 OnDisconnected 來安全地處理資源。 如果刷子在斷開後又被使用,則會再次呼叫 OnConnected 。
建構函式
| 名稱 | Description |
|---|---|
| XamlCompositionBrushBase() |
提供 XamlCompositionBrushBase 衍生類別的基底類別初始化行為。 |
屬性
| 名稱 | Description |
|---|---|
| CompositionBrush |
取得或設定這個 XAML 筆刷所使用的 CompositionBrush 。 |
| Dispatcher |
它總是在 Windows 應用程式 SDK 應用程式中回傳 |
| DispatcherQueue |
得到 |
| FallbackColor |
如果 CompositionBrush 無法渲染,該用來渲染的顏色。 |
| FallbackColorProperty |
識別 FallbackColor 依賴屬性。 |
| Opacity |
取得或設定 筆刷的不透明度程度。 (繼承來源 Brush) |
| RelativeTransform |
取得或設定使用相對座標套用到筆刷上的轉換。 (繼承來源 Brush) |
| Transform |
取得或設定套用到畫筆上的轉換。 (繼承來源 Brush) |