WPF中含Image的ItemTemplateSelector引发WebRequest致UI冻结求助
WPF中ItemTemplateSelector加载图片触发WebRequest导致UI冻结的问题分析
问题描述
在WPF中使用包含Image的ItemTemplateSelector时,部分机器上随机出现.Net框架发起100秒超时的WebRequest,直接导致UI冻结。图片已作为项目资源部署,触发时机为包含TemplateSelector的视图构造函数执行阶段。
相关代码
DataTemplate示例
<DataTemplate x:Key="ExampleDataTemplate"> <StackPanel> <Image Width="16" Height="16" RenderOptions.BitmapScalingMode="HighQuality" Source="pack://application:,,,/UI.Shared;component/Resources/ExampleImage.ico" /> </StackPanel> </DataTemplate>
ItemTemplateSelector逻辑
public override DataTemplate SelectTemplate(object item, DependencyObject container) { var libItem = item as FunctionViewAdapter; if (libItem == null) return Unknown; switch (libItem.Type) { case MyType.Example: return Example; default: return Unknown; } }
调用栈
KernelBase.dll!00007fffc653f310() Unknown WindowsBase.ni.dll!00007fff7073069e() Unknown [Managed to Native Transition] WindowsBase.dll!System.Windows.Threading.DispatcherSynchronizationContext.Wait(System.IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) Unknown [Native to Managed Transition] [Managed to Native Transition] mscorlib.dll!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle waitableSafeHandle, long millisecondsTimeout, bool hasThreadAffinity, bool exitContext) Unknown mscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) Unknown System.dll!System.Net.FileWebRequest.GetResponse() Unknown > PresentationCore.dll!MS.Internal.WpfWebRequestHelper.GetResponse(System.Net.WebRequest request) Unknown PresentationCore.dll!System.Windows.Media.ColorContext.Initialize(System.Uri profileUri, bool isStandardProfileUriNotFromUser) Unknown PresentationCore.dll!System.Windows.Media.ColorContext.ColorContext(System.Windows.Media.PixelFormat pixelFormat) Unknown PresentationCore.dll!System.Windows.Media.Imaging.BitmapSource.CreateCachedBitmap(System.Windows.Media.Imaging.BitmapFrame frame, System.Windows.Media.Imaging.BitmapSourceSafeMILHandle wicSource, System.Windows.Media.Imaging.BitmapCreateOptions createOptions, System.Windows.Media.Imaging.BitmapCacheOption cacheOption, System.Windows.Media.Imaging.BitmapPalette palette) Unknown PresentationCore.dll!System.Windows.Media.Imaging.BitmapFrameDecode.FinalizeCreation() Unknown PresentationCore.dll!System.Windows.Media.Imaging.BitmapDecoder.SetupFrames(System.Windows.Media.Imaging.BitmapDecoder decoder, System.Collections.ObjectModel.ReadOnlyCollection<System.Windows.Media.Imaging.BitmapFrame> frames) Unknown PresentationCore.dll!System.Windows.Media.Imaging.BitmapDecoder.Frames.get() Unknown PresentationCore.dll!System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(System.Uri baseUri, System.Uri uri, System.IO.Stream stream, System.Windows.Media.Imaging.BitmapCreateOptions createOptions, System.Windows.Media.Imaging.BitmapCacheOption cacheOption, System.Net.Cache.RequestCachePolicy uriCachePolicy) Unknown PresentationCore.dll!System.Windows.Media.ImageSourceConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) Unknown PresentationFramework.dll!System.Windows.TemplateContent.TrySharingValue(System.Windows.DependencyProperty property, object value, string parentName, System.Xaml.XamlReader xamlReader, bool allowRecursive, out System.Windows.PropertyValue? sharedValue) Unknown PresentationFramework.dll!System.Windows.TemplateContent.TrySharingProperty(System.Xaml.XamlReader xamlReader, System.Xaml.XamlType parentType, string parentName, MS.Utility.FrugalObjectList<System.Xaml.NamespaceDeclaration> previousNamespaces, out System.Windows.PropertyValue? sharedValue) Unknown PresentationFramework.dll!System.Windows.TemplateContent.ParseNode(System.Xaml.XamlReader xamlReader, System.Windows.TemplateContent.StackOfFrames stack, System.Collections.Generic.List<System.Windows.PropertyValue> sharedProperties, ref int nameNumber, out object newValue) Unknown PresentationFramework.dll!System.Windows.TemplateContent.ParseNodes(System.Windows.TemplateContent.StackOfFrames stack, System.Collections.Generic.List<System.Windows.PropertyValue> sharedProperties, ref int nameNumber) Unknown PresentationFramework.dll!System.Windows.TemplateContent.ParseTree(System.Windows.TemplateContent.StackOfFrames stack, System.Collections.Generic.List<System.Windows.PropertyValue> sharedProperties, ref int nameNumber) Unknown PresentationFramework.dll!System.Windows.TemplateContent.ParseXaml() Unknown PresentationFramework.dll!System.Windows.FrameworkTemplate.Template.set(System.Windows.TemplateContent value) Unknown System.Xaml.dll!MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(object inst, System.Xaml.XamlMember property, object value) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(MS.Internal.Xaml.Context.ObjectWriterContext ctx, System.Xaml.XamlMember prop, object value, bool onParent) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(MS.Internal.Xaml.Context.ObjectWriterContext ctx) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.WriteEndMember() Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter, bool onlyLoadOneNode, bool skipJournaledProperties, bool shouldPassLineNumberInfo, System.Xaml.IXamlLineInfo xamlLineInfo, System.Xaml.IXamlLineInfoConsumer xamlLineInfoConsumer, MS.Internal.Xaml.Context.XamlContextStack<System.Windows.Markup.WpfXamlFrame> stack, System.Windows.Markup.IStyleConnector styleConnector) Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader xamlReader, System.Xaml.IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, System.Xaml.XamlObjectWriterSettings settings, System.Uri baseUri) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.CreateObject(System.Windows.Baml2006.KeyRecord key) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.OnGettingValue(object key, ref object value, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.OnGettingValuePrivate(object key, ref object value, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValueWithoutLock(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValue(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.DeferredResourceReference.GetValue(System.Windows.BaseValueSourceInternal valueSource) Unknown PresentationFramework.dll!System.Windows.StaticResourceExtension.TryProvideValueImpl(System.IServiceProvider serviceProvider, bool allowDeferredReference, bool mustReturnDeferredResourceReference) Unknown PresentationFramework.dll!System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider serviceProvider, bool allowDeferredReference) Unknown System.Xaml.dll!MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(System.Windows.Markup.MarkupExtension me, System.IServiceProvider serviceProvider) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_ProvideValue(MS.Internal.Xaml.Context.ObjectWriterContext ctx) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(MS.Internal.Xaml.Context.ObjectWriterContext ctx) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.WriteEndMember() Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter, bool onlyLoadOneNode, bool skipJournaledProperties, bool shouldPassLineNumberInfo, System.Xaml.IXamlLineInfo xamlLineInfo, System.Xaml.IXamlLineInfoConsumer xamlLineInfoConsumer, MS.Internal.Xaml.Context.XamlContextStack<System.Windows.Markup.WpfXamlFrame> stack, System.Windows.Markup.IStyleConnector styleConnector) Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader xamlReader, System.Xaml.IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, System.Xaml.XamlObjectWriterSettings settings, System.Uri baseUri) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.CreateObject(System.Windows.Baml2006.KeyRecord key) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.OnGettingValue(object key, ref object value, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.OnGettingValuePrivate(object key, ref object value, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValueWithoutLock(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValue(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValueWithoutLock(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValue(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValueWithoutLock(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.GetValue(object key, out bool canCache) Unknown PresentationFramework.dll!System.Windows.ResourceDictionary.Lookup(object key, bool allowDeferredResourceReference, bool mustReturnDeferredResourceReference, bool canCacheAsThemeResource) Unknown PresentationFramework.dll!System.Windows.StaticResourceExtension.FindResourceInEnviroment(System.IServiceProvider serviceProvider, bool allowDeferredReference, bool mustReturnDeferredResourceReference) Unknown PresentationFramework.dll!System.Windows.StaticResourceExtension.TryProvideValueImpl(System.IServiceProvider serviceProvider, bool allowDeferredReference, bool mustReturnDeferredResourceReference) Unknown PresentationFramework.dll!System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider serviceProvider, bool allowDeferredReference) Unknown System.Xaml.dll!MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(System.Windows.Markup.MarkupExtension me, System.IServiceProvider serviceProvider) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_ProvideValue(MS.Internal.Xaml.Context.ObjectWriterContext ctx) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(MS.Internal.Xaml.Context.ObjectWriterContext ctx) Unknown System.Xaml.dll!System.Xaml.XamlObjectWriter.WriteEndMember() Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter, bool onlyLoadOneNode, bool skipJournaledProperties, bool shouldPassLineNumberInfo, System.Xaml.IXamlLineInfo xamlLineInfo, System.Xaml.IXamlLineInfoConsumer xamlLineInfoConsumer, MS.Internal.Xaml.Context.XamlContextStack<System.Windows.Markup.WpfXamlFrame> stack, System.Windows.Markup.IStyleConnector styleConnector) Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader xamlReader, System.Xaml.IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, System.Xaml.XamlObjectWriterSettings settings, System.Uri baseUri) Unknown PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader xamlReader, bool skipJournaledProperties, object rootObject, System.Xaml.Permissions.XamlAccessLevel accessLevel, System.Uri baseUri) Unknown PresentationFramework.dll!System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream stream, System.Windows.Markup.ParserContext parserContext, object parent, bool closeStream) Unknown UI.Display.dll!UI.Display.Views.Display.DisplayView() Unknown
原因分析
从调用栈和现象可以定位到核心触发点与WPF的颜色上下文初始化逻辑相关,具体原因如下:
WIC组件的颜色配置文件加载异常:WPF依靠Windows Imaging Component(WIC)解析图片,当处理ICO文件时,WIC会尝试加载系统默认颜色配置文件。若系统中该配置文件的路径被错误解析为网络路径,或系统颜色配置存储位置存在权限/损坏问题,就会触发
FileWebRequest。Pack URI资源解析的环境差异:虽然图片是嵌入的项目资源,但部分机器上WPF的URI解析逻辑可能出现异常,将pack URI误识别为需要通过WebRequest访问的路径,这通常与系统的URI处理组件配置或版本差异有关。
UI线程的同步阻塞:模板加载和图片解析均在UI线程执行,
FileWebRequest.GetResponse()是同步阻塞调用,默认超时100秒,直接导致UI线程冻结。特定系统配置触发:问题仅随机出现在部分机器,说明与目标机器的特定配置相关——比如系统颜色管理设置异常、WIC组件版本不兼容、系统权限限制导致无法读取本地颜色配置文件,进而触发框架的网络请求降级逻辑。
内容的提问来源于stack exchange,提问作者user3292642
相关产品推荐
相关产品推荐

