示例#1
0
        public static void SetDefaultStyle(this FrameworkContentElement element, ResourceDescriptor descriptor)
        {
            if (FrameworkElementFindResourceMethod == null)
            {
                throw new InvalidOperationException("Invalid framework version");
            }
            var resource = FrameworkElementFindResourceMethod.Invoke(null, new object[] { element, null, element.GetType() });

            if (resource == DependencyProperty.UnsetValue)
            {
                element.Style = GetResource <Style>(descriptor);
            }
        }
示例#2
0
        public static void SetDefaultStyle(Type elementType, ResourceDescriptor descriptor)
        {
            if (Runtime.IsDesignMode)
            {
                return;
            }
            var stylePropertyMetadata = (FrameworkPropertyMetadata)FrameworkElement.StyleProperty.GetMetadata(elementType);

            FrameworkElement.StyleProperty.OverrideMetadata(elementType,
                                                            new FrameworkPropertyMetadata
            {
                PropertyChangedCallback = stylePropertyMetadata.PropertyChangedCallback,
                CoerceValueCallback     = stylePropertyMetadata.CoerceValueCallback,
                DefaultValue            = GetResource <Style>(descriptor)
            });
        }
示例#3
0
        public static object GetResource(this ResourceDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            var element = new FrameworkElement();

            element.Resources.MergedDictionaries.Add(new SharedResourceDictionary
            {
                Source = descriptor.ResourceDictionary
            });
            var result = element.FindResource(descriptor);

            if (descriptor.ResourceType.IsInstanceOfType(result))
            {
                return(result);
            }
            throw new InvalidOperationException("Resource not found");
        }
示例#4
0
 public static T GetResource <T>(this ResourceDescriptor descriptor)
 {
     return((T)GetResource(descriptor));
 }