Windows runtime 相互依赖的Windows运行时依赖项属性

Windows runtime 相互依赖的Windows运行时依赖项属性,windows-runtime,dependency-properties,c++-cx,Windows Runtime,Dependency Properties,C++ Cx,我得到了一个用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);

我得到了一个用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));
}
uint8\u t DemoControl::Red::get()
{
返回静态_cast(GetValue(RedProperty));
}
void DemoControl::Red::set(uint8\u t r)
{
设置值(红色属性,r);
}
DependencyProperty ^DemoControl::\u redProperty=
DependencyProperty::Register(“红色”,
uint\u t::typeid,
DemoControl::typeid,
ref新属性元数据(127,ref新属性更改回调(&DemoControl::OnRedChanged));
void DemoControl::OnRedChanged(DependencyObject ^d,DependencyPropertyChangedEventArgs ^e)
{
DemoControl^DemoControl=static\u cast(d);
DemoControl->renderer->SetRed(static_cast(e->NewValue));
}
第四个属性返回整个颜色,即它是其他三个属性值的组合

问题是,如果红色、绿色或蓝色属性发生更改而不触发通过数据绑定附加到颜色属性的代码,我将如何更新该颜色属性


除了WPF之外,还提出了类似的问题。答案建议使用,但这似乎是Windows运行时组件无法使用的功能。注册依赖属性时使用的属性元数据> /COD>对象不支持从我所看到的<代码> CoerceValueCallback <代码>。< /P> < P>我不是C++专家,但至少对于您的场景,我想我可以帮助。

您可以为多个依赖项属性注册相同的回调。因此,在您的特定情况下,您可以只更改一个
OnColorComponentChanged
回调:

void DemoControl::OnColorComponentChanged(DependencyObject ^d,DependencyPropertyChangedEventArgs ^e)
{
DemoControl^DemoControl=static\u cast(d);
if(e->Property==DemoControl::RedProperty)//此处的HandWave语法
{
DemoControl->renderer->SetRed(static_cast(e->NewValue));
}
如果(e->Property==DemoControl::GreenProperty)
{
DemoControl->renderer->SetGreen(static_cast(e->NewValue));
}
如果(e->Property==DemoControl::BlueProperty)
{
DemoControl->renderer->SetBlue(static_cast(e->NewValue));
}
//在这里挥手
DemoControl->Color=MakeColor(DemoControl->红色,DemoControl->绿色,DemoControl->蓝色);
}

感谢您的意见,非常感谢。您的方法适用于表示各个颜色通道的三个属性,即它们可以共享相同的回调。但是,组合颜色属性的回调需要更新各个颜色通道属性。为了防止无限循环,在无限循环中它们会不断地相互更新,我将回调拆分,并使用布尔值来防止连续回调被调用。我把这个问题留了一会儿。也许有人想出了一个我不知道的Windows运行时机制。如果不是,我就发布我的解决方案。是的,对于相反的情况,防护布尔值是有意义的。虽然它不会直接帮助您,但请随时在网站上提交建议。我认为,如果我们能在将来为这些类型的场景集思广益,想出一些帮助者或其他东西,那就太好了。