如何注册应与值类型一起使用的 DependencyProperty
How to register a DependencyProperty that shall work with a value type
在 C++/CX 中 class 我想创建一个 DependencyProperty
来获取或设置一个普通的 C++ float
值类型,但我不知道我需要如何指定DependencyProperty::Register
.
期望的类型
我举个例子。如果我要使用与引用类型一起使用的 属性,我会将以下内容放入我的 C++/CX class:
的头文件中
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))
可以直接使用float::typeid
注册float类型
DependencyProperty^ MyCustomClass::_backgroundColorProperty =
DependencyProperty::Register("BackgroundColor",
float::typeid,
MyCustomClass::typeid,
ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));
此外,从这个document,它提到:
If you want a DP with a floating-point type, then make it double
(Double in MIDL 3.0). Declaring and implementing a DP of type float
(Single in MIDL), and then setting a value for that DP in XAML markup,
results in the error Failed to create a 'Windows.Foundation.Single'
from the text ''.
所以我建议你使用双依赖 属性 而不是浮动。
在 C++/CX 中 class 我想创建一个 DependencyProperty
来获取或设置一个普通的 C++ float
值类型,但我不知道我需要如何指定DependencyProperty::Register
.
我举个例子。如果我要使用与引用类型一起使用的 属性,我会将以下内容放入我的 C++/CX class:
的头文件中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))
可以直接使用float::typeid
注册float类型
DependencyProperty^ MyCustomClass::_backgroundColorProperty =
DependencyProperty::Register("BackgroundColor",
float::typeid,
MyCustomClass::typeid,
ref new PropertyMetadata(1.0f, ref new PropertyChangedCallback(&MyCustomClass::OnBackgroundColorChanged)));
此外,从这个document,它提到:
If you want a DP with a floating-point type, then make it double (Double in MIDL 3.0). Declaring and implementing a DP of type float (Single in MIDL), and then setting a value for that DP in XAML markup, results in the error Failed to create a 'Windows.Foundation.Single' from the text ''.
所以我建议你使用双依赖 属性 而不是浮动。