johankson / awesome

Using Font Awesome in Xamarin Forms
16 stars 10 forks source link

Setting Label Text using a static resource in the code behind #5

Open npagare opened 5 years ago

npagare commented 5 years ago

Hi Johan, thank you sharing this repo.

I am trying to set provide a static resource in the OnPropertyChanged event for setting the static resource to the Label.Text by concatenating the value of a bindable property using a stringbuilder

e.g. from your repo , inside the property changed event

            BaseIcon.Style = (Style)Application.Current.Resources["FontAwesomeWeb550Regular"];

            StringBuilder baseIconSb = new StringBuilder();
            baseIconSb.Append("\"");
            baseIconSb.Append("{x:Static helpers:FontAwesomeWeb550Font.");
            baseIconSb.Append(BaseIconSrc);
            baseIconSb.Append("}");
            baseIconSb.Append("\"");

            BaseIcon.Text = baseIconSb.ToString();

But, when rendered, it shows the text value in the label starting "{x ….. depending on the side of the label width set.

image

Any thought, what I may be missing ?

Thanks

Polarts commented 5 years ago

When you set the text via code-behind, it takes the string literally as is to display rather than run it on the XAML compilation engine. Thus, your string is presented plainly as "{x:Static helpers:FontAwesomeWeb550Font.[BaseIconSrc]}" rather than fetching the resource from the dictionary and presenting it.

What you should do in this case is: BaseIcon.Text = (string)Application.Current.Resources["FontAwesomeWeb550Font"+BaseIconSrc];

Something along these lines.