/// <summary> /// 验证 <see cref="MarkupExtension"/> 中此时提供的服务是否正在向 <see cref="FrameworkElement"/> 的依赖属性赋值。 /// </summary> /// <param name="extension">需要进行通用验证的 <see cref="MarkupExtension"/>。</param> /// <param name="serviceProvider">在 <see cref="MarkupExtension.ProvideValue"/> 方法中传入的参数。</param> /// <param name="element">如果正在向 <see cref="FrameworkElement"/> 提供值,则返回此 <see cref="FrameworkElement"/> 实例。</param> /// <returns>如果验证为此时可以赋值,则返回 true;如果因为设计时支持或者服务对象不存在,则返回 false。</returns> internal static bool ValidateMarkupExtension(this MarkupExtension extension, IServiceProvider serviceProvider, out FrameworkElement element) { if (extension == null) { throw new ArgumentNullException(nameof(extension)); } if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } var extensionName = extension.GetType().Name; // 设计时直接创建新实例。 if (DesignerProperties.IsInDesignModeProperty.DefaultMetadata.DefaultValue is true) { element = null; return(false); } // 如果没有服务,则直接返回。 if (!(serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget service)) { element = null; return(false); } // MarkupExtension 在样式模板中,返回 this 以延迟提供值。 if (service.TargetObject.ToString().EndsWith("SharedDp", StringComparison.Ordinal)) { throw new NotSupportedException($"样式和模板不支持 {extensionName},请在 XAML 次根级元素中使用。"); } if (service.TargetObject is SetterBase) { throw new NotSupportedException($"属性 Setter.Value 不支持 {extensionName},请在 XAML 次根级元素中使用。"); } if (!(service.TargetObject is FrameworkElement frameworkElement)) { throw new NotSupportedException("只有 FrameworkElement 才能支持使用 ViewModel 设置 DataContext。"); } element = frameworkElement; return(true); }
// Token: 0x060064CC RID: 25804 RVA: 0x001C4434 File Offset: 0x001C2634 internal static void CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IServiceProvider serviceProvider, out DependencyObject targetDependencyObject, out DependencyProperty targetDependencyProperty) { targetDependencyObject = null; targetDependencyProperty = null; IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValueTarget == null) { return; } object targetObject = provideValueTarget.TargetObject; if (targetObject == null) { return; } Type type = targetObject.GetType(); object targetProperty = provideValueTarget.TargetProperty; if (targetProperty != null) { targetDependencyProperty = (targetProperty as DependencyProperty); if (targetDependencyProperty != null) { targetDependencyObject = (targetObject as DependencyObject); return; } MemberInfo memberInfo = targetProperty as MemberInfo; if (memberInfo != null) { PropertyInfo propertyInfo = memberInfo as PropertyInfo; EventHandler <XamlSetMarkupExtensionEventArgs> eventHandler = Helper.LookupSetMarkupExtensionHandler(type); if (eventHandler != null && propertyInfo != null) { IXamlSchemaContextProvider xamlSchemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if (xamlSchemaContextProvider != null) { XamlSchemaContext schemaContext = xamlSchemaContextProvider.SchemaContext; XamlType xamlType = schemaContext.GetXamlType(type); if (xamlType != null) { XamlMember member = xamlType.GetMember(propertyInfo.Name); if (member != null) { XamlSetMarkupExtensionEventArgs xamlSetMarkupExtensionEventArgs = new XamlSetMarkupExtensionEventArgs(member, markupExtension, serviceProvider); eventHandler(targetObject, xamlSetMarkupExtensionEventArgs); if (xamlSetMarkupExtensionEventArgs.Handled) { return; } } } } } Type type2; if (propertyInfo != null) { type2 = propertyInfo.PropertyType; } else { MethodInfo methodInfo = (MethodInfo)memberInfo; ParameterInfo[] parameters = methodInfo.GetParameters(); type2 = parameters[1].ParameterType; } if (!typeof(MarkupExtension).IsAssignableFrom(type2) || !type2.IsAssignableFrom(markupExtension.GetType())) { throw new System.Windows.Markup.XamlParseException(SR.Get("MarkupExtensionDynamicOrBindingOnClrProp", new object[] { markupExtension.GetType().Name, memberInfo.Name, type.Name })); } } else if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection <BindingBase>).IsAssignableFrom(targetProperty.GetType())) { throw new System.Windows.Markup.XamlParseException(SR.Get("MarkupExtensionDynamicOrBindingInCollection", new object[] { markupExtension.GetType().Name, targetProperty.GetType().Name })); } } else if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection <BindingBase>).IsAssignableFrom(type)) { throw new System.Windows.Markup.XamlParseException(SR.Get("MarkupExtensionDynamicOrBindingInCollection", new object[] { markupExtension.GetType().Name, type.Name })); } }
public override object CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider) { object obj3; try { obj3 = me.ProvideValue(serviceProvider); } catch (Exception exception) { if (CriticalExceptions.IsCriticalException(exception)) { throw; } throw this.CreateException(System.Xaml.SR.Get("ProvideValue", new object[] { me.GetType() }), exception); } return(obj3); }
internal static void CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IServiceProvider serviceProvider, out DependencyObject targetDependencyObject, out DependencyProperty targetDependencyProperty) { targetDependencyObject = null; targetDependencyProperty = null; var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValueTarget == null) { return; } var targetObject = provideValueTarget.TargetObject; if (targetObject == null) { return; } var type = targetObject.GetType(); var targetProperty = provideValueTarget.TargetProperty; if (targetProperty != null) { targetDependencyProperty = targetProperty as DependencyProperty; if (targetDependencyProperty != null) { targetDependencyObject = targetObject as DependencyObject; } else { var memberInfo = targetProperty as MemberInfo; if (memberInfo != null) { var propertyInfo = memberInfo as PropertyInfo; EventHandler <XamlSetMarkupExtensionEventArgs> eventHandler = LookupSetMarkupExtensionHandler(type); if (eventHandler != null && propertyInfo != null) { var schemaContextProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if (schemaContextProvider != null) { var xamlType = schemaContextProvider.SchemaContext.GetXamlType(type); if (xamlType != null) { var member = xamlType.GetMember(propertyInfo.Name); if (member != null) { var e = new XamlSetMarkupExtensionEventArgs(member, markupExtension, serviceProvider); eventHandler(targetObject, e); if (e.Handled) { return; } } } } } var c = !(propertyInfo != null) ? ((MethodBase)memberInfo).GetParameters()[1].ParameterType : propertyInfo.PropertyType; if (!typeof(MarkupExtension).IsAssignableFrom(c) || !c.IsAssignableFrom(markupExtension.GetType())) { throw new System.Windows.Markup.XamlParseException(__SR.Get("MarkupExtensionDynamicOrBindingOnClrProp", markupExtension.GetType().Name, memberInfo.Name, type.Name)); } } else if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection <BindingBase>).IsAssignableFrom(targetProperty.GetType())) { throw new System.Windows.Markup.XamlParseException(__SR.Get("MarkupExtensionDynamicOrBindingInCollection", markupExtension.GetType().Name, targetProperty.GetType().Name)); } } } else if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection <BindingBase>).IsAssignableFrom(type)) { throw new System.Windows.Markup.XamlParseException(__SR.Get("MarkupExtensionDynamicOrBindingInCollection", markupExtension.GetType().Name, type.Name)); } }