WPF ValueConverter 错误处理的最佳实践是什么?

发布于 2024-08-25 16:28:22 字数 705 浏览 11 评论 0原文

应该实现两个功能,我想知道我需要对输入进行什么类型的验证以及如何管理错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-09-01 16:28:22

我个人检查类型和值,然后进行转换。如果由于任何原因无法转换,那么我只需返回未转换的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.

忆沫 2024-09-01 16:28:22

我只是想知道为什么需要在转换器中捕获异常?你有例子吗?

在我看来,应该没有必要这样做,并且对于可空类型之类的东西,应该在转换器中正确处理它们,因为其他所有事情都应该在调用转换器之前处理。
即,如果用户在数字字段中输入字符串,则应由验证而不是转换器捕获该字符串。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文