《算术运算中的溢出或下溢》WPF具体问题我的WPF测试应用程序(很简单,只有一个窗口)正在使用第三方托管的dll(例如X.dll)。此托管dll使用一些非托管dll。所以我想写一个只引用X.dll的小wpf应用程序。在窗口的构造函数中,我访问了X.dll中的某些内容(即在X.dll中的某个命名空间中)。在执行此操作时,我没有发现任何异常,而且似乎一切都在按预期进行。但是在将控制权返回到.NET运行时后,我在Application类的“DispatcherUnhandledException”处理程序中遇到异常:“算术运算中发生溢出或下溢。”System.ArithmeticException未处理Message=""发生在算术运算上溢或下溢"。来自=“PresentationFramework”StackTrace:System.Windows.Window.ValidateTopLeft(Doublelength)System.Windows.Window.CoerceTop(DependencyObjectd,Objectvalue)System.Windows.DependencyObject.ProcessCoerceValue(DependencyPropertydp,PropertyMetadatametadata,EntryIndex&entryIndex,Int32&targetIndex,EffectiveValueEntry&newEntry,EffectiveValueEntry&oldEntry,Object&oldValue,ObjectbaseValue,CoerceValueCallbackcoerceValueCallback,BooleancoerceWithDeferredReference,BooleanskipBaseValueChecks)System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndexentryIndex,DependencyPropertydp,PropertyMetadata元数据,EffectiveValueEntryoldEntry,EffectiveValueEntry&newReerce操作,BooleoperationType)System.Windows.DependencyObject.CoerceValue(DependencyPropertydp)在System.Windows.Window.SetupInitialState(DoublerequestedTop,DoublerequestedLeft,DoublerequestedWidth,DoublerequestedHeight)System.Windows.Window.CreateSourceWindowImpl()在System.Windows.Window.SafeCreateWindow()在System.Windows.Window.ShowHelper(ObjectbooleanBox)System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegatecallback,Objectargs,BooleanisSingleParameter)System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Objectsource,Delegatecallback,Objectargs,BooleanisSingleParameter,DelegatecatchHandler)几点:谁能猜到问题所在?谢谢,Mishal根据@Mishhl的链接,修复是publicstaticvoidAction(){//重置浮点数(从外部应用程序调用时出现溢出异常)_fpreset();这是由于包含的DLL中的某些内容将FP重置为与WPF/C#/MicrosoftDLLs不兼容的状态,Delphi/CPPB默认情况下会执行此操作。所以要么FloatingPointReset.Action();在窗口构造函数或App()构造函数中您可能需要更改以下内容以引用任何版本的msvcr###。dll[DllImport("msvcr110.dll",CallingConvention=CallingConvention.Cdecl)]例如[DllImport("msvcr70.dll",CallingConvention=CallingConvention.Cdecl)]我不确定根本原因,但解决方案在这里:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a31f9c7a-0e15-4a09-a544-bec07f0f152c似乎是一个流行的错误?感谢Michal如果它能提供帮助,我遇到了完全相同的问题(如此处和此处所述)。我带来了一个可能的原因。我将ElementHost(c#、.net4.0、winform、VS2012)用于一个不错的WPFUserControl。一切都很好。在我的UserControl代码隐藏中,我使用从另一个DLL编码的类来存储特定任务的进度。例如(接近现实):publicclassSI_MyProgressionHelper{publicstringgstr_OverallProgress_Description{get;放;}publicintgint_OverallProgress_ActualValue{得到;放;}publicintgint_OverallProgress_MaximumValue{得到;WPFprogressBar/textBox来报告进度。但是(这是另一个对话),WPF需要将INotifyPropertyChanged应用于要插入到WPF中的每个属性(以自动更新WPF中的控件)。这是我见过的最丑陋的东西之一,但它就是这样(我知道我一定遗漏了一些东西......)无论如何,为了纪念我的ProgressBar/TextBox和我的SI_MyProgressionHelper类机制之间的WPF绑定,我已经修改了类(我重新,在另一个DLL中)到这个:publicclassSI_MyProgressionHelper:INotifyPropertyChanged{privatestring_gstr_OverallProgress_Description="";公共字符串gstr_OverallProgress_Description{get{return_gstr_OverallProgress_Description;}设置{_gstr_OverallProgress_Description=值;RaisePropertyChanged("gstr_OverallProgress_Description");}}privateint_gint_OverallProgress_ActualValue=0;publicintgint_OverallProgress_ActualValue{get{return_gint_OverallProgress_ActualValue;}设置{_gint_OverallProgress_ActualValue=值;RaisePropertyChanged("gint_OverallProgress_ActualValue");}}私有int_gint_OverallProgress_MaximumValue=9999;publicintgint_OverallProgress_MaximumValue{得到{返回_gint_OverallProgress_MaximumValue;}设置{_gint_OverallProgress_MaximumValue=值;RaisePropertyChanged("gint_OverallProgress_MaximumValue");}}protectedvirtualvoidRaisePropertyChanged(stringpropName){if(PropertyChanged!=null)PropertyChanged(this,newPropertyChangedEventArgs(propName));}公共事件PropertyChangedEventHandlerPropertyChanged发生在我的任何UserCon中;XMLElement(高度,宽度等)在Program.csMain()方法中:[DllImport("msvcr70.dll",CallingConvention=CallingConvention.Cdecl)]publicstaticexternint_fpreset();然后,在我的UserControl.xaml.cs中,在constructor:publicUserControl1(){Program._fpreset();InitializeComponent();}另外,这就是我写这条评论的原因:通过在我的Helper类上删除INotifyPropertyChanged(和其他相关内容),问题消失了。(但是我的WPF很难过,因为我的代码对他来说太短了。希望这些额外的信息能帮助遇到同样问题的人。另外,对于我所有的法国同事,这里有一个法语错误:Ilyaeuundédassementdecapacitépositifounégatifdansl'opérationarithmétique.以上就是C#学习教程:《算术运算中的上溢或下溢》WPF具体问题的全部内容。如果对你有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络合集,不代表立场,如涉及侵权,请点击有权联系管理员删除。如需转载请注明出处:
