GeorgeR
Topic Author
Posts: 39
Joined: 15 Mar 2018, 01:06

C++ DependencyProperty types

29 Mar 2018, 12:10

Shouldn't my string be an NsString (I get an error if I use this) and i'm getting an error with ImageSource. What's the correct setup?

https://pastebin.com/0sV17D68
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: C++ DependencyProperty types

29 Mar 2018, 15:37

For strings you must use the NsString class when registering the DependencyProperty.
Data->RegisterProperty<NsString>(LabelProperty, "Label", FrameworkPropertyMetadata::Create(FrameworkOptions_None));
const char* AttributeControl::GetLabel() const
{
    return Noesis::ContentControl::GetValue<NsString>(LabelProperty).c_str();
}
 
void AttributeControl::SetLabel(const char* label)
{
    Noesis::ContentControl::SetValue<NsString>(LabelProperty, label);
}
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: C++ DependencyProperty types

02 Apr 2018, 12:19

And for the ImageSource you have to specify it like this:
Data->RegisterProperty<Ptr<ImageSource>>(IconProperty, "Icon", FrameworkPropertyMetadata::Create(Ptr<ImageSource>(), FrameworkOptions_None));
Noesis::ImageSource* AttributeControl::GetIcon() const
{
    return Noesis::DependencyObject::GetValue<Ptr<ImageSource>>(IconProperty);
}
 
void AttributeControl::SetIcon(Noesis::ImageSource* imageSource)
{
    Noesis::DependencyObject::SetValue<Ptr<ImageSource>>(IconProperty, imageSource);
}
We have to improve this code to hide NsString and Ptr inside DependencyObject implementation. So users deal only with basic types like const char* and ImageSource* when doing RegisterProperty and GetValue/SetValue.
 
GeorgeR
Topic Author
Posts: 39
Joined: 15 Mar 2018, 01:06

Re: C++ DependencyProperty types

02 Apr 2018, 12:39

I made some helpers that could be integrated, although it's not the best for char* (non-const):
#pragma once

#define NS_DECLARE_PROPERTY(PropertyType, PropertyName)										\
	static const Noesis::DependencyProperty* PropertyName##Property;						\
	const PropertyType Get##PropertyName() const;											\
	void Set##PropertyName(PropertyType PropertyName);		

#define NS_DEFINE_PROPERTY(ClassName, PropertyType, PropertyName)							\
	const DependencyProperty* ClassName::PropertyName##Property;							\
	const PropertyType ClassName::Get##PropertyName() const									\
	{																						\
		return Noesis::Control::GetValue<PropertyType>(PropertyName##Property);				\
	}																						\
	void ClassName::Set##PropertyName(PropertyType PropertyName)							\
	{																						\
		Noesis::Control::SetValue<PropertyType>(PropertyName##Property, PropertyName);		\
	}
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: C++ DependencyProperty types

03 Apr 2018, 08:13

Thanks for the macros. They neither work with Ptr<> properties. But it will work in the future when we change them to something like:
ImageSource* AttributeControl::GetIcon() const
{
    return DependencyObject::GetValue<ImageSource*>(IconProperty);
}
 
void AttributeControl::SetIcon(ImageSource* imageSource)
{
    DependencyObject::SetValue<ImageSource*>(IconProperty, imageSource);
}
This is a breaking change, so won't happen soon. Honestly I am not sure about how to deal with strings, because I think that char* and NsString are different things.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 89 guests