WPF ValueConverter 错误处理的最佳实践是什么?
应该实现两个功能,我想知道我需要对输入进行什么类型的验证以及如何管理错误。
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
End Function
我需要检查值的类型和参数的类型是否是我所期望的。
如果不是,我是否应该不返回任何内容并在另一个地方捕获错误的行为?或者我应该在这里抛出异常或断言?
这里有什么最佳或建议的做法可以应用吗?
Two function should be implemented, and I am wondering what type of validation I need to do on inputs and how to manage errors.
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
End Function
I need to check if the type of value and the type of parameter are what I am expecting.
In case they are not, should I return nothing and catch the wrong behavior in another place? Or should I throw here an exception or an assert?
Is there any best or suggested practices to apply here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人检查类型和值,然后进行转换。如果由于任何原因无法转换,那么我只需返回未转换的
value
。我不会抛出任何异常或进行任何日志记录 - 大多数时候很容易看出转换是否失败,因为您在 UI 中看不到预期的项目。在转换器中记录错误可能会非常昂贵,因此我避免它。数据绑定(通常)会默默地失败(我曾经在 Silverlight 中与转换器进行过绑定,一旦出现错误),您可以检查“输出”窗口是否有任何错误,并且在以下情况下在转换器中敲击条件断点非常简单:调试任何问题所必需的。
Personally i check for the type and value, then convert it. If for any reason it cannot be converted then i simply return
value
unconverted. I don't throw any exceptions or do any logging - most of the time it is easy to see if a conversion has failed because you don't see the expected item in the UI. Logging errors in a converter could be quite expensive, hence why i avoid it.The data binding (usually) fails silently (i have had binding with a converter throw a big massive fit once in Silverlight), you can check the Output window for any errors, and it is pretty simple to whack a conditional breakpoint in the converter if necessary to debug any issues.
我只是想知道为什么需要在转换器中捕获异常?你有例子吗?
在我看来,应该没有必要这样做,并且对于可空类型之类的东西,应该在转换器中正确处理它们,因为其他所有事情都应该在调用转换器之前处理。
即,如果用户在数字字段中输入字符串,则应由验证而不是转换器捕获该字符串。
I just wanted to find out why you need to catch an exception in the converter? Have you got an example?
In my opinion there should be no need for this, and with something like nullable types, these should be handled in the converter correctly, as everything else should be handled before the converter is called.
I.e. if the user is entering a string into a numeric field, this should be caught by validation and not the converter.