samedi 26 avril 2014

WPF CustomControls and Resources


Vote count:

0




I often struggle with the best strategy to fullfil what I'd like to do with resources.



  • I'd like to be able to "override" a set of default resources (mostly colors) with others at runtime (not necessarily on the fly, but at startup before windows are created, load a set of configured ResourceDictionaries).

  • I'd like to still use StaticResource as often as possible

  • I'd like to organize my custom controls and resources in a logical way


Meeting all three desires seems tough. Using DynamicResource makes it easier to meet the first and last, which gets me closest. I worry a little about performance since that's something constantly mentioned about DynamicResource (though I understand Blend uses DynamicResources and doesn't seem to suffer). There are other reasons it's not ideal. It feels odd to use a named resource in a control, with no guarantee it's ever actually defined. I've also seen a couple of strange bugs with DynamicResources. Where for some reason, the lookup fails and the Brush is just set to null, just randomly in one window but not another. So I've shied from going the "all DynamicResource all the time" route.


I was looking at the beautiful Reference Source site and looking at how SystemColors brushes are defined. It seems like doing something similar would allow me to load my resources from whereever I want, still have everything work in the designer, provide default values, etc. Pretty much satisfying all three bullets above. Are there some downsides to this approach? It seems like i could even still leverage defining them in a private xaml file, and just copy them from that ResourceDictionary when defining the properties in my own SystemColors type class.


Is this crazy?



asked 36 secs ago






Aucun commentaire:

Enregistrer un commentaire