前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 类型的构造函数执行符合指定的绑定约束的调用时引发了异常

WPF 类型的构造函数执行符合指定的绑定约束的调用时引发了异常

作者头像
林德熙
发布2019-04-22 15:33:24
4.5K0
发布2019-04-22 15:33:24
举报
文章被收录于专栏:林德熙的博客林德熙的博客

本文告诉大家如果遇到类型“Foo.MainWindow”的构造函数执行符合指定的绑定约束的调用时引发了异常的时候可以如何知道是哪个不清真代码

在 WPF 开发中,如果遇到类型的构造函数执行符合指定的绑定约束的调用时引发了异常,那么此时通过调用堆栈里面是看不到自己的代码的

代码语言:javascript
复制
	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)	
 	PresentationFramework.dll!System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader xamlReader, bool skipJournaledProperties, object rootObject, System.Xaml.Permissions.XamlAccessLevel accessLevel, System.Uri baseUri)	
 	PresentationFramework.dll!System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream stream, System.Windows.Markup.ParserContext parserContext, object parent, bool closeStream)	
 	PresentationFramework.dll!System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream stream, System.Windows.Markup.ParserContext pc)	
 	PresentationFramework.dll!System.Windows.Application.LoadComponent(System.Uri resourceLocator, bool bSkipJournaledProperties)	
 	PresentationFramework.dll!System.Windows.Application.DoStartup()	
 	PresentationFramework.dll!System.Windows.Application..ctor.AnonymousMethod__1_0(object unused)	
 	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)	
 	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler)	
 	WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl()	
 	WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state)	
 	WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj)	
 	mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)	
 	mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)	
 	mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)	
 	WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state)	
 	WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke()	
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue()	
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)	
 	WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled)	
 	WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o)	
 	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs)	
 	WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler)	
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)	
 	WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam)	
 	[本机到托管的转换]	
 	[托管到本机的转换]	
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame)	
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame)	
 	PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore)	
 	PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window)	
 	PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window)	
 	PresentationFramework.dll!System.Windows.Application.Run()	
 	CelakercalbochallhiNerjufeeqalchelfu.exe!CelakercalbochallhiNerjufeeqalchelfu.App.Main()	

但是此时应该可以找到一些内部异常

很经常可以看到的内部异常有两个

  • “Foo.MainWindow”的类型初始值设定项引发异常。
  • ArgumentException: 默认值类型与属性“Lindexi”类型不匹配。

如果看到是这两个异常,那么请找到默认值类型与属性“Lindexi”类型不匹配里面说到的属性名对应的定义的代码,一般这个属性是依赖属性或附加属性

如我就逗比写了这段代码

代码语言:javascript
复制
        public static readonly DependencyProperty LindexiProperty =
            DependencyProperty.Register("Lindexi", typeof(string), typeof(MainWindow), new PropertyMetadata(0));

那么上面的代码有什么问题,在依赖属性的定义,需要在 PropertyMetadata 传入的默认参数的类和定义的 typeof(string) 是相同的类,如上面代码定义的是字符串,但是在默认值设置的是整数,于是这里就不能转换了。注意,即使隐式转换也是不可以的,如定义的是浮点但是传入整数也是不可以的

解决方法是修改默认值或修改定义的类就可以了

那么为什么在这里定义不对会直接告诉小伙伴是在构造函数绑定的时候炸了?因为定义的是静态字段,在静态字段是会在整个类构造函数之前就执行,于是你就无法在构造函数添加断点找到是哪个不清真代码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档