gpt4 book ai didi

wpf - 样式 TargetType 在未附加到调试器时会导致 XamlParseException

转载 作者:行者123 更新时间:2023-12-03 18:10:29 27 4
gpt4 key购买 nike

我在几个不同的 WPF 应用程序中使用了一组非常简单的样式。我将此样式存储在一个通用项目的 Xaml 文件中,然后通过合并到 Resources 中添加在 App.xaml在每个项目中。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<Style TargetType="dxe:ComboBoxEdit">
<Setter Property="AutoComplete" Value="True" />
<Setter Property="IncrementalFiltering" Value="True" />
<Setter Property="ImmediatePopup" Value="True" />
<Setter Property="IsTextEditable" Value="True" />
<Setter Property="ClearSelectionOnBackspace" Value="True" />
</Style>
<Style TargetType="dxe:ComboBoxEditSettings">
<Setter Property="AutoComplete" Value="True" />
<Setter Property="IncrementalFiltering" Value="True" />
<Setter Property="ImmediatePopup" Value="True" />
<Setter Property="IsTextEditable" Value="True" />
</Style>
</ResourceDictionary>

不幸的是,这导致了 XamlParseException关于 TargetType属性(property),但 仅当未附加到调试器时 .如果我在调试器中启动应用程序,一切都很好。如果我“不调试就开始”,我会得到 App.xaml正在加载:
System.Windows.Markup.XamlParseException: 'Failed to create a 'TargetType' from the text 'dxe:ComboBoxEdit'.' Line number '5' and line position '12'. ---> System.Xaml.XamlParseException: Type reference cannot find type named '{http://schemas.devexpress.com/winfx/2008/xaml/editors}ComboBoxEdit'.
at MS.Internal.Xaml.Context.ObjectWriterContext.ServiceProvider_Resolve(String qName)
at MS.Internal.Xaml.ServiceProviderContext.System.Windows.Markup.IXamlTypeResolver.Resolve(String qName)
at System.Xaml.Replacements.TypeTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateObjectWithTypeConverter(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateFromValue(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value, XamlMember property)
at System.Xaml.XamlObjectWriter.Logic_CreateFromValue(ObjectWriterContext ctx, XamlValueConverter`1 typeConverter, Object value, XamlMember property, String targetName, IAddLineInfo lineInfo)
--- 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)
at Shell.App.InitializeComponent() in c:\DevProjects\CoreApplication\Shell\App.xaml:line 1
at Shell.App.Main() in C:\DevProjects\CoreApplication\Shell\obj\x86\Debug\App.g.cs:line 0

如果我注释掉这两个 Style节点,然后一切正常。有任何想法吗?

最佳答案

我遇到了同样的问题,对我来说,这是将资源文件添加到项目中的方式。

我必须确定 每一个我的 xaml 样式资源文件的一部分被设置为“页面”(在“构建操作”属性中)。默认情况下,它们并非都处于这种模式(一些作为“内容”,另一些作为“编译”或“嵌入资源”或“资源”),这导致了这种问题。

也许对你来说也是一样...

编辑:据我所知,它与 xaml 代码如何嵌入项目有关,特别是 WPF 解析它的顺序:如果源文件设置为“页面”,则字典的顺序不考虑合并,因此这将在 Release模式下工作:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- let's say this dictionary contains a call to "MyButtonStyle" ... -->
<ResourceDictionary Source="resources/Common.xaml" />
<!-- ... and this one contains the definition of "MyButtonStyle" -->
<ResourceDictionary Source="resources/GeneralResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

但这不适用于其他选项(不是所有选项,我没有尝试每一个选项),因为调用将在定义之前解析。

至于为什么它在 Debug模式下而不是在 Release模式下工作(或者在你没有调试的情况下开始时),我猜这是因为 WPF 根据你所处的模式(调试与否)以不同的方式解析和存储资源到内存中.我认为 WPF 在 Debug模式下将所有内容存储在内存中,而在 Release模式下仅存储所需的内容(例如调用代码而不是定义代码),但话又说回来,这只是我的猜测......

编辑 2:对我来说,调试是一场噩梦,因为它更糟糕:它在某些发布配置中工作,而不是在其他配置中工作,因为根据用户采取的操作的顺序,他会得到错误或没有(资源可能调用时是否已经在内存中收费,具体取决于此时 WPf 已经需要什么),当然我不能说“调试”,因为 Debug模式总是有效......我花了一段时间才弄清楚这个解决方案......很高兴它有帮助

关于wpf - 样式 TargetType 在未附加到调试器时会导致 XamlParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com