【发布时间】:2020-08-31 10:31:27
【问题描述】:
我有一个用 C++/CX 编写的 Windows 运行时组件,它包含四个依赖项属性。其中三个属性在底层渲染器中将颜色通道设置为红色、绿色和蓝色。此类属性的 C++/C 代码如下所示:
uint8_t DemoControl::Red::get()
{
return static_cast<uint8_t>(GetValue(RedProperty));
}
void DemoControl::Red::set(uint8_t r)
{
SetValue(RedProperty, r);
}
DependencyProperty^ DemoControl::_redProperty =
DependencyProperty::Register("Red",
uint_t::typeid,
DemoControl::typeid,
ref new PropertyMetadata(127, ref new PropertyChangedCallback(&DemoControl::OnRedChanged)));
void DemoControl::OnRedChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
{
DemoControl^ DemoControl = static_cast<DemoControl^>(d);
DemoControl->renderer->SetRed(static_cast<uint8_t>(e->NewValue));
}
第四个属性返回整个颜色,即它是其他三个属性值的组合。
问题是,如果红色、绿色或蓝色属性发生变化而不触发通过数据绑定附加到颜色属性的代码,我将如何更新该颜色属性?
here 提出了类似的问题,但针对 WPF。答案建议使用value coercion,但这似乎是 Windows 运行时组件不可用的功能。注册依赖属性时使用的PropertyMetadata对象不支持CoerceValueCallback,据我所知。
【问题讨论】:
标签: windows-runtime dependency-properties c++-cx