开发者

Attached Dependency Property vs. Dictionary - Silverlight

开发者 https://www.devze.com 2023-03-07 04:19 出处:网络
first post.So first off, thanks for all the help over the years as I\'ve learned from the sidelines.I just have a rather specific code-design question that I couldn\'t find elsewhere.

first post. So first off, thanks for all the help over the years as I've learned from the sidelines. I just have a rather specific code-design question that I couldn't find elsewhere.

I have a series of storyboards (among other things,) that relate to specific FrameworkElements, that need to be generated in the code, and I feel that it's either a little messy, and possibly a little slow (thought I haven't tested) to store a dictionary with the FrameworkElement - Storyboard relationships, to lookup on the fly.

    private static Dictionary<FrameworkElement, Storyboard> storyboardMapping;

    private void FrameworkElement_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        FrameworkElement fe = sender as FrameworkElement;
        Storyboard sb = null;

        if(storyboardMapping.TryGetValue(fe, out sb))
        {
            sb.Begin()开发者_JS百科;
        }
        // etc
    }

Having just considered using (private) Attached Dependency Properties instead, does anyone have an opinion on whether one is faster/cleaner than the other? The other downside I see to using Dictionaries, is that they also don't work well with WeakReferences. Not being a C# Guru, I don't completely understand whether that creates issues with Garbage Collection.

Also, I'm not exactly sure how big this Dictionary could get. Possibly up to 400 objects or even more?


I wouldn't worry about the memory footprint of the dictionary. They are just references.

However, there is a danger of having long lived objects (static) contain references to short lived objects (UI elements) as this keeps them from being garbage collected.


Rather than using either, I am now using the .Resources property of UIElements which is a much more appropriate place to store these.

This question was previously answered, however the answer seems to have disappeared. As such I will mark this answer as the correct one unless the original reappears...

0

精彩评论

暂无评论...
验证码 换一张
取 消