WPF ResourceDictionary StringFormat XamlParseException

时间:2013-04-03 14:07:47

标签: wpf wpf-4.0

我有一个定义了Style的ResourceDictionary,它有一个带有Binding StringFormat的ControlTemplate,如下所示:

<TextBlock Text="{Binding TotalCash, Mode=OneWay, StringFormat={}{0:N2}, FallbackValue=0.00}" />

我在本地运行,一切都很好,但我在服务器上进行构建和安装,当我启动任何使用ResourceDictionary的WFP屏幕时,我得到以下异常:

System.Windows.Markup.XamlParseException: 'Unexpected token after end of markup extension.' Line number '224' and line position '148'. ---> System.Xaml.XamlParseException: 'Unexpected token after end of markup extension.' Line number '224' and line position '148'.
   at MS.Internal.Xaml.Parser.MePullParser.<Parse>d__0.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<LogicStream_Attribute>d__6f.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_EmptyElement>d__14.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_PropertyContent>d__5f.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_NonemptyPropertyElement>d__42.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_PropertyElement>d__35.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementContent>d__4a.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_ElementBody>d__23.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<P_Element>d__7.MoveNext()
   at MS.Internal.Xaml.Parser.XamlPullParser.<Parse>d__0.MoveNext()
   at MS.Internal.Xaml.NodeStreamSorter.ReadAheadToEndOfAttributes()
   at MS.Internal.Xaml.NodeStreamSorter.ReadAheadAndSortCtorProperties()
   at MS.Internal.Xaml.NodeStreamSorter.MoveNext()
   at System.Xaml.XamlXmlReader.Read()
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   --- End of inner exception stack trace ---
   at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)

我做了一些挖掘并通过将ResourceDictionary StringFormat从{} {0:N2}更改为N2来实现它:

<TextBlock Text="{Binding TotalCash, Mode=OneWay, StringFormat=N2, FallbackValue=0.00}" />

以下作品放在UserControl中:

<TextBlock Text="{Binding TotalCash, Mode=OneWay, StringFormat={}{0:N2}, FallbackValue=0.00}" />

我的问题是为什么相同的StringFormat在UserControl中工作但不在ResourceDictionary中工作?我很高兴它正在工作,但想了解原因。

1 个答案:

答案 0 :(得分:3)

这是WPF bug

我猜你在开发机器上本地安装了.NET 4.5(可能是因为安装了Visual Studio 2012),但是你的服务器上没有安装.NET 4.5(只有.NET 4)。因此,您的服务器没有Microsoft在.NET 4.5中添加到WPF的错误修正。