Page 1 of 1

C++ DependencyProperty types

Posted: 29 Mar 2018, 12:10
by GeorgeR
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

Re: C++ DependencyProperty types

Posted: 29 Mar 2018, 15:37
by jsantos
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);
}

Re: C++ DependencyProperty types

Posted: 02 Apr 2018, 12:19
by sfernandez
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.

Re: C++ DependencyProperty types

Posted: 02 Apr 2018, 12:39
by GeorgeR
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);		\
	}

Re: C++ DependencyProperty types

Posted: 03 Apr 2018, 08:13
by jsantos
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.