避免在StyleHelper.OnBindingValueInTemplateChanged中的NullReferenceException崩溃

时间:2022-03-07 16:54:49

My application had crashed several times with the following stack trace:

我的应用程序已经使用以下堆栈跟踪崩溃了几次:

An uncatched exception was thrown by application: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.StyleHelper.OnBindingValueInTemplateChanged(Object sender, BindingValueChangedEventArgs e)
   at System.Windows.Data.BindingExpressionBase.ChangeValue(Object newValue, Boolean notify)
   at System.Windows.Data.MultiBindingExpression.TransferValue()
   at System.Windows.Data.MultiBindingExpression.Transfer()
   at System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)
   at System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)
   at System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

The problem is that this happens only in production environment and occurs rarely - about 2-3 times in the past month. So I can no possibility to debug it and I have only the logs. The crash occurred after the system navigated to another state (I see in the log that the next state view and view model are both loaded). The stack trace doesn't point at the specific place.

问题是这种情况只发生在生产环境中并且很少发生 - 过去一个月大约2-3次。所以我无法调试它,我只有日志。系统导航到另一个状态后发生崩溃(我在日志中看到下一个状态视图和视图模型都已加载)。堆栈跟踪不指向特定位置。

I did some investigation of this issue, and here're my findings:

我对这个问题进行了一些调查,这是我的发现:

The source code of the StyleHelper class contains a comment above this method: "Is Invoked when a binding in a condition of a data trigger changes its value". And in the call stack written in log I see MultiBindingExpression so I guess this issue is related to MultiDataTrigger. The only place we use MultiDataTrigger in our xamls (related to this particular state) is this style. It also looks (from the OnBindingValueInTemplateChanged code) that the only candidates to be null are a sender (that is BindingExpression) or the TargetElement of this expression (because another variables have a null-check and if there was a crash in any method called from this method so it had been writted to the call stack). I really don't understand in which scenario two of these friends could be null...

StyleHelper类的源代码包含此方法上方的注释:“在数据触发条件中的绑定更改其值时调用”。在日志中编写的调用堆栈中,我看到MultiBindingExpression,所以我猜这个问题与MultiDataTrigger有关。我们在xamls中使用MultiDataTrigger的唯一地方(与此特定状态相关)就是这种风格。它还看起来(从OnBindingValueInTemplateChanged代码),唯一的null候选者是发送者(即BindingExpression)或此表达式的TargetElement(因为另一个变量具有空检查,并且如果在任何调用的方法中发生崩溃这个方法所以它已被写入调用堆栈)。我真的不明白在哪种情况下这两个朋友可能是空的...

Did anyone face the same exception before and can advise how it can happen? Or any suggestions how I can avoid the application to crash?

之前有没有人面对同样的例外,可以告诉它如何发生?或者我有什么建议可以避免应用程序崩溃?

1 个解决方案

#1


0  

Try using try/catch in the Convert method of your IMultiValueConverter.

尝试在IMultiValueConverter的Convert方法中使用try / catch。

#1


0  

Try using try/catch in the Convert method of your IMultiValueConverter.

尝试在IMultiValueConverter的Convert方法中使用try / catch。