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

问题描述 投票:2回答:1

我有一个用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));
}

第四个属性返回整个颜色,即它是其他三个属性的值的组合。

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

[W0,有人问过类似的问题here。答案建议使用value coercion,但这似乎是Windows运行时组件不可用的功能。据我所见,注册依赖项属性时使用的PropertyMetadata对象不支持CoerceValueCallback

windows-runtime dependency-properties c++-cx
1个回答
0
投票

我不是C ++专家,但至少对于您的情况,我认为我可以提供帮助。

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

void DemoControl::OnColorComponentChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
{
  DemoControl^ DemoControl = static_cast<DemoControl^>(d);

  if (e->Property == DemoControl::RedProperty) // Hand Wave Syntax Here
  {
    DemoControl->renderer->SetRed(static_cast<uint8_t>(e->NewValue));
  }
  else if (e->Property == DemoControl::GreenProperty)
  {
    DemoControl->renderer->SetGreen(static_cast<uint8_t>(e->NewValue));  
  }
  else if (e->Property == DemoControl::BlueProperty)
  {
    DemoControl->renderer->SetBlue(static_cast<uint8_t>(e->NewValue));  
  }

  // Hand Wave Here
  DemoControl->Color = MakeColor(DemoControl->Red, DemoControl->Green, DemoControl->Blue);
}
© www.soinside.com 2019 - 2024. All rights reserved.