Freigeben über


System.Windows.Markup.XamlParseException with WPF application in Visual Studio 2015

I recently had a customer report that sample WPF code from the Chapter 10: Resources chapter of the APRESS book 'Pro WPF in C#' was throwing an exception when compiled and run only under the Visual Studio 2015 debugger:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  LineNumber=8
  LinePosition=32
  Message='Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '32'.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadDeferredContent(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings parentSettings, Uri baseUri)
       at System.Windows.ResourceDictionary.CreateObject(KeyRecord key)
       at System.Windows.ResourceDictionary.OnGettingValue(Object key, Object& value, Boolean& canCache)
       at System.Windows.ResourceDictionary.OnGettingValuePrivate(Object key, Object& value, Boolean& canCache)
       at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache)
       at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache)
       at System.Windows.DeferredThemeResourceReference.GetValue(BaseValueSourceInternal valueSource)
       at System.Windows.DependencyObject.GetEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, RequestFlags requests)
       at System.Windows.DependencyObject.GetValueEntry(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, RequestFlags requests)
       at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
       at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
       at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
       at System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
       at System.Windows.FrameworkTemplate.<>c__DisplayClass0.<LoadOptimizedTemplateContent>b__5(Object sender, XamlObjectEventArgs args)
       at System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value)
       at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
       at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException:
       HResult=-2146232800
       Message=Cannot locate resource 'resourcelibrary;v1.0.5703.26795;themes/resourcelibrary;component/sadface.jpg'.
       Source=PresentationFramework
       StackTrace:
            at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
            at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
            at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
            at System.IO.Packaging.PackWebResponse.GetResponseStream()
            at System.IO.Packaging.PackWebResponse.get_ContentType()
            at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
            at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
            at System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy)
            at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
            at System.Windows.Baml2006.TypeConverterMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
       InnerException:

This same application, when run outside of the debugger runs without issue.

A bit of debugging turned up that the environment block of the application when run under the debugger has an extra environment variable set:

     ENABLE_XAML_DIAGNOSTICS_SOURCE_INFO=1

With that discovery I was able to set this environment variable outside of the debugger and also reproduce the issue, verifying it as the root cause.

A bit of online research on this turned up this article - blogs.msdn.com/b/wpf/archive/2015/02/24/expanding-wpf-for-ui-debugging.aspx indicating this is a new feature of the WPF framework for .NET 4.6.

I was able to further determine that this can be turned on and off in the debugger from the Tools menu, Options, Debugging, 'Enable UI Debugging Tools for XAML' property, allowing an straight-forward work around for the exception.

After reporting this to WPF product team they verified it is a bug in WPF and is under consideration for a fix in an future .NET hotfix rollup