~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to class/Microsoft.SilverlightControls/Controls/Data/src/DataGrid/ResourceHelper.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
Import upstream version 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System; 
 
2
using System.Collections.Generic;
 
3
using System.Windows.Controls;
 
4
using System.Windows.Markup; 
 
5
 
 
6
namespace System.Windows.Controls
 
7
 
8
    internal sealed class ResourceHelper 
 
9
    {
 
10
        static Dictionary<string, string> _cache = new Dictionary<string, string>(); 
 
11
 
 
12
        private ResourceHelper()
 
13
        { 
 
14
        }
 
15
 
 
16
        static public ControlTemplate GetControlTemplate<T>() 
 
17
        { 
 
18
            return GetControlTemplate(typeof(T));
 
19
        } 
 
20
 
 
21
        static public ControlTemplate GetControlTemplate(Type type)
 
22
        { 
 
23
            return GetControlTemplate(type, type.FullName);
 
24
        }
 
25
 
 
26
        static public ControlTemplate GetControlTemplate(Type type, string resourceName) 
 
27
        {
 
28
            string xaml = ResourceHelper.GetTemplateXaml(type, resourceName); 
 
29
 
 
30
            if (String.IsNullOrEmpty(xaml))
 
31
            { 
 
32
                throw new Exception(type.Name + " xaml could not be loaded");
 
33
            }
 
34
            else 
 
35
            { 
 
36
#if DEBUG
 
37
                try 
 
38
                {
 
39
#endif
 
40
                    ControlTemplate result = (ControlTemplate)XamlReader.Load(xaml); 
 
41
 
 
42
                    return result;
 
43
#if DEBUG 
 
44
                } 
 
45
                catch
 
46
                { 
 
47
                    throw;
 
48
                }
 
49
#endif 
 
50
            }
 
51
        }
 
52
 
 
53
        static public string GetTemplateXaml(Type type, string resourceName) 
 
54
        {
 
55
            string template; 
 
56
 
 
57
            if (!_cache.TryGetValue(resourceName, out template))
 
58
            { 
 
59
                System.IO.Stream s = type.Assembly.GetManifestResourceStream(resourceName + ".xaml");
 
60
                if (s != null)
 
61
                { 
 
62
                    template = new System.IO.StreamReader(s).ReadToEnd(); 
 
63
                    _cache[resourceName] = template;
 
64
                } 
 
65
            }
 
66
 
 
67
            return template; 
 
68
        }
 
69
    }
 
70