Daki
Topic Author
Posts: 57
Joined: 16 Aug 2013, 00:48

DependencyProperty with string value

05 Sep 2013, 05:33

What is the proper way to implement a DependencyProperty that represents a string value? I have a simple UserControl object that is basically just some text blocks whose text I want to expose with Dependency Properties so I may set it programmatically. I have these properties as NsStrings right now but I get compilation errors when using NsProp.

Should the values be cast to NsStrings, or should it be another type? Do the function pointers provided to NsProp need to be of a certain signature? Right now for my setter I am passing in a NsString which I don't really want to do (I'd rather pass in a const NsString& or an NsString*).

I'm curious what the best way to do this is - the examples only seem to cover numeric types.
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: DependencyProperty with string value

06 Sep 2013, 00:26

The functions passed to NsProp must use const NsString&:
const NsString& MyClass::GetText() const
{
    return GetValue<NsString>(TextProperty);
}

void MyClass::SetText(const NsString& text)
{
    SetValue<NsString>(TextProperty, text);
}

...
NsProp("Text", &MyClass::GetText, &MyClass::SetText);
The property is registered using the NsString type:
data->RegisterProperty<NsString>(TextProperty, "Text",
    FrameworkPropertyMetadata::Create(NsString(), FrameworkOptions_None));
 
Daki
Topic Author
Posts: 57
Joined: 16 Aug 2013, 00:48

Re: DependencyProperty with string value

06 Sep 2013, 01:00

Thanks! :)
 
Shorinji
Posts: 24
Joined: 16 Aug 2013, 19:45
Location: Shanghai

Re: DependencyProperty with string value

06 Sep 2013, 02:39

Hi,
I have a question which could be related this.
I also have some UserControl contaning some strings.
Right now i am using string not NsString, and it's working. Is it correct to use string for dependcy properties in Unity?
Thanks in advance.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: DependencyProperty with string value

06 Sep 2013, 12:21

Hi,
I have a question which could be related this.
I also have some UserControl contaning some strings.
Right now i am using string not NsString, and it's working. Is it correct to use string for dependcy properties in Unity?
Thanks in advance.
Yes, mono strings are automatically converted to native NsString, so that is the correct way to define a string property in Unity.
 
Shorinji
Posts: 24
Joined: 16 Aug 2013, 19:45
Location: Shanghai

Re: DependencyProperty with string value

06 Sep 2013, 16:19

ok thanks :)

Who is online

Users browsing this forum: No registered users and 1 guest