Topic Author
Posts: 5
Joined: 07 Oct 2014, 22:00

Accessing Style setters in Unity C#

28 Oct 2014, 21:51

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.

User avatar
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Accessing Style setters in Unity C#

30 Oct 2014, 17:13


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;

Who is online

Users browsing this forum: No registered users and 1 guest