如何注册将使用值类型的DependencyProperty

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

在C ++ / CX类中,我想创建一个获取或设置普通C ++ DependencyProperty值类型的float,但我只是想不出如何指定DependencyProperty::Register期望的类型。

让我举个例子。如果要使用与引用类型兼容的属性,请将以下内容放入C ++ / CX类的头文件中:

public:

  static property Windows::UI::Xaml::DependencyProperty^ BackgroundColorProperty
  {
    Windows::UI::Xaml::DependencyProperty^ get();
  };

  property Windows::UI::Xaml::Media::Brush^ BackgroundColor
  {
    Windows::UI::Xaml::Media::Brush^ get();
    void set(Windows::UI::Xaml::Media::Brush^ value);
  };

private:

  static Windows::UI::Xaml::DependencyProperty^ _backgroundColorProperty;

  static void OnBackgroundColorChanged(Windows::UI::Xaml::DependencyObject^ d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ e);

实现看起来像这样:

DependencyProperty^ MyCustomClass::BackgroundColorProperty::get()
{
  return _backgroundColorProperty;
}

Windows::UI::Xaml::Media::Brush^ MyCustomClass::BackgroundColor::get()
{
  return static_cast<Windows::UI::Xaml::Media::Brush^>(GetValue(BackgroundColorProperty));
}

void MyCustomClass::BackgroundColor::set(Windows::UI::Xaml::Media::Brush^ value)
{
  SetValue(BackgroundColorProperty, value);
}

DependencyProperty^ MyCustomClass::_backgroundColorProperty =
  DependencyProperty::Register("BackgroundColor",
                               Brush::typeid,
                               MyCustomClass::typeid,
                               ref new PropertyMetadata(ref new SolidColorBrush(Windows::UI::ColorHelper::FromArgb(255, 255, 255, 255)), ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));

void MyCustomClass::OnBackgroundColorChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
{
  MyCustomClass^ myClass = static_cast<MyCustomClass^>(d);
  // Do whatever needs to be done
}

这全部有效。 Visual Studio的XAML设计器可以识别该属性,并且一切都会按预期工作。

但是可以说我想要一个使用float而不是Brush的属性。我需要如何在DependencyProperty::Register方法中指定必要的类型?我的意思是下面的代码中的第二个参数。

DependencyProperty^ MyCustomClass::_backgroundColorProperty =
  DependencyProperty::Register("BackgroundColor",
                               /* How do I specify the type of the float here?*/,
                               MyCustomClass::typeid,
                               ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));

我尝试了以下无效的方法:

ref new TypeKind(TypeKind::Primitive, float32)
typeof(float32)
typeof(float)
typeid(float)
ref new Windows::UI::Xaml::Interop::TypeKind(TypeKind::Primitive, float)
ref new Windows::UI::Xaml::Interop::TypeKind(TypeKind::Primitive, typeid(float))
winrt-xaml uwp-xaml c++-cx
1个回答
1
投票
您可以直接使用float::typeid注册浮点类型。

DependencyProperty^ MyCustomClass::_backgroundColorProperty = DependencyProperty::Register("BackgroundColor", float::typeid, MyCustomClass::typeid, ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));

另外,从该document中提及:

如果要使用浮点类型的DP,则将其加倍(在MIDL 3.0中为Double)。声明和实现float类型的DP(在MIDL中为单个),然后在XAML标记中为该DP设置一个值,导致错误无法创建“ Windows.Foundation.Single”从文字“。

所以我建议您使用双重依赖属性而不是float。

© www.soinside.com 2019 - 2024. All rights reserved.