Silverlight Shared MergedDictionaries

前端 未结 4 659
囚心锁ツ
囚心锁ツ 2021-02-10 23:27

I am using Silverlight 4 and trying to share some common styles (colors, brushes). My take was to put them into a \"Common.xaml\" Resource Dictionary and then use it in all othe

4条回答
  •  天涯浪人
    2021-02-10 23:57

    I was able to tweak the solution proposed at http://www.wpftutorial.net/MergedDictionaryPerformance.html to make it work with Silverlight and the VS designer (haven't tried Blend). I have a blog post on it here (http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/)

    public class SharedResourceDictionary : ResourceDictionary
    {
        public static Dictionary _sharedDictionaries =
           new Dictionary();
    
        private Uri _sourceUri;
        public new Uri Source
        {
            get { return _sourceUri; }
            set
            {
                _sourceUri = value;
                if (!_sharedDictionaries.ContainsKey(value))
                {
                    Application.LoadComponent(this, value);
                    _sharedDictionaries.Add(value, this);
                }
                else
                {
                    CopyInto(this, _sharedDictionaries[value]);
                }
            }
        }
    
        private static void CopyInto(ResourceDictionary copy, ResourceDictionary original)
        {
            foreach (var dictionary in original.MergedDictionaries)
            {
                var mergedCopy = new ResourceDictionary();
                CopyInto(mergedCopy, dictionary);
                copy.MergedDictionaries.Add(mergedCopy);
            }
            foreach (DictionaryEntry pair in original)
            {
                copy.Add(pair.Key, pair.Value);
            }
        }
    }
    

    XAML usage:

    
        
    
    

提交回复
热议问题