0

I have been watching some online xamarin forms tutorials. All of them suggest using the resource dictionary for global rules such as styles. However I have noticed a lot of risks of mistakes. Consider the following example:

In the dictionary I mention

<Style TargetType="Label" x:Key="lblStyle">
    <Property="FontAttributes" Value="Bold" />
    <Setter Property="FontSize" Value="30" />
</Style> 

Then I can use it as

<Label Text="My Text" Style="{StaticResource lblStyle}" />

However when assigning to the dictionary, I only get intellisense some key words and when implementing on the label I don't get intellisense for the key lblStyle. Also I don't know if I have entered anything wrong as it is mostly not caught in compile time. I do understand why no intellisense, but I found the following easier to code and it does the same

First I create a class

public class MyLabel : Label
{
    public MyLabel()
    {
        FontAttributes = FontAttributes.Bold;
        FontSize = 30;
    }

}

I can then declare in xml and use following:

<Components:MyLabel Text="My Text" />

In terms of coding, the second way seems to be better in every way. What I do not understand is, why all the tutorials recommend we use the resource dictionary? Is there some serious performance advantage that is worth all the weak typing?

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
  • It's not about performance, it's about separation of concerns. Classes are for code, XAML is for UI. It has a lot of benefits - the main three are Maintainability, Testability and Blendability. – Zohar Peled Oct 09 '17 at 05:25
  • Ok, I get your UI separation point but I didn't get any of the three benefits. How can it be maintainable if for each change I need to look up and copy paste the property & values? How is there a difference in testability or blendability when both can be called in the same way? – Neville Nazerane Oct 09 '17 at 05:39
  • Tha'ts too long for a comment, and currently I don't have the time to write a good answer. You can start by reading [this question and it's answers](https://stackoverflow.com/questions/1644453/why-mvvm-and-what-are-its-core-benefits). – Zohar Peled Oct 09 '17 at 05:56
  • Just want to mention that performance is impacted here. The static resource lays in memory once and can be reused all the time. This safes a lot of performance in the long run especially when it comes to many controls and styles. Also this can be used for theming (switching the complete style of an app). – Alexander Schmidt Oct 09 '17 at 06:05
  • well regarding the performance impact, I did think of that. But for that I can always create a static readonly style variables which would even have a better performance than a dictionary with key value pairs. The theme can still be done. If needed, all classes could even go into the same file. – Neville Nazerane Oct 09 '17 at 06:25
  • Strange - I DO get intellisense for the key lblStyle – Steve Chadbourne Oct 09 '17 at 06:37
  • you are getting intellisense for a dictionary's key? That is really strange. Where have you saved your resource dictionary? – Neville Nazerane Oct 09 '17 at 06:38
  • I never hard code font sizes - I define a size say LargeFont of 30 in the dictionary then refer to that from labels etc also in the dictionary. If the customer asks to change the font to 28 I can do it in one place. – Steve Chadbourne Oct 09 '17 at 06:39
  • I save my resource dictionary in app.xaml – Steve Chadbourne Oct 09 '17 at 06:40
  • what version of vs? enterprise? – Neville Nazerane Oct 09 '17 at 06:40
  • VS2015 Professional – Steve Chadbourne Oct 09 '17 at 06:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156240/discussion-between-steve-chadbourne-and-neville-nazerane). – Steve Chadbourne Oct 09 '17 at 06:42

0 Answers0