Page 1 of 1

Accessing Style setters in Unity C#

Posted: 28 Oct 2014, 21:51
by herbss
What is the correct way to access the setter value strings in Unity C#?

<Style x:Key="SampleStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Regular"></Setter>
<Setter Property="FontSize" Value="18"></Setter>

var style = FindResource<Noesis.Style>("SampleStyle");
var setters = style.GetSetters();
for (int i = 0; i < setters.Count(); i++)
var setter = setters.Get((uint)i).As<Setter>();
Debug.Log(string.Format("{0}: {1} {2}",setter.GetProperty().GetName(),setter.GetValue().GetType(), setter.GetTargetName()));

When I call setter.GetValue I get a Noesis.BaseComponent. I am interested in getting the value of 18 for font size in code.


Re: Accessing Style setters in Unity C#

Posted: 30 Oct 2014, 17:13
by sfernandez

Setter values are boxed inside a BaseComponent object. I'm sorry to tell current 1.1.12 API only allows to unbox strings, using the AsString() method:
BaseComponent value = setter.GetValue();
We can add support for other basic types in a following release.

Anyway, the upcoming 1.2 version will deal with this in a transparent way, because we removed BaseComponent from the API, and everything is converted automatically from a to C# object, being able to do things like this:
float fontSize = setter.Value as float;