如何处理propertygrid抛出的异常?
我有一个 C# 中的 Winforms PropertyGrid,它控制从嵌入式系统获取和设置的数据。
我将有关设备的查询(GetFrequency、SetPowerLimit、SetACCurrent ec)编写为属性,以便我可以绑定数据,而无需为我想要创建的表单中的每个命令编写另一个方法。
PropertyGrid 工作得非常好,我的意思是它显示了我需要的所有数据并允许我更改它们的值。但当然并非总是如此。
由于必须工作的设备处于黑匣子状态,程序在某些时候会正常停止。 (即当您关闭设备时。)
例如,这是我必须经常从设备读取的值。 “Simin”
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
在 Getters 和 Setters 中,我有这些 get 方法来建立电脑和设备之间的连接。它们是我大多数情况下例外的实际来源。
像这样
if (!_port.IsOpen)
{
throw new HuettingerException(Localisation.Error_PortClosed);
}
或这样
// Read first 4 bytes
if (_port.Read(inputv, 0, 4) != 4)
{
throw new HuettingerException(Localisation.Error_NoConnection);
}
所以我有三个抽象层,当我尝试读取一些数据时,我从机器端获取异常(如上面所示)。我试图在开始时捕获其中的一些,例如
public DeviceUI()
{
InitializeComponent();
try
{
propertyGrid1.SelectedObject = device;
}
catch (TimeoutException te) // i tried other exceptions too
{
MessageBox.Show(te.Message);
}
在 winform 初始化时,但它不起作用。
我的问题是,如果它是按钮或文本字段,我可以轻松捕获事件方法中的异常,但我不知道如何处理 PropertyGrid 引发的异常。
有什么意见吗?
I have a Winforms PropertyGrid in c# that controls the data that getted and setted from an embedded system.
I wrote the queries about the device (GetFrequency,SetPowerLimit,SetACCurrent e.c.) as Properties so that i can bind the data without writing another method for each command inside the Form that i want to create.
The PropertyGrid works really nice, i mean it shows all the data that i need and lets me change their values. But not always of course.
Because of this black box situtation of the device that have to work on, the program halts normally at some points. (i.e. when you turn the device off.)
For example, here is a value that i have to read frequently from the device. "Simin"
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
Within the Getters and Setters i have these get methods that establishes the connection between pc and the device. And they are the actual source of exceptions that i take mostly.
Like this
if (!_port.IsOpen)
{
throw new HuettingerException(Localisation.Error_PortClosed);
}
or this
// Read first 4 bytes
if (_port.Read(inputv, 0, 4) != 4)
{
throw new HuettingerException(Localisation.Error_NoConnection);
}
So i have three abstraction layers and i take the exceptions from the machine side (like the one shown above) while i try to read some data. I tried to catch some of them at the beginning like
public DeviceUI()
{
InitializeComponent();
try
{
propertyGrid1.SelectedObject = device;
}
catch (TimeoutException te) // i tried other exceptions too
{
MessageBox.Show(te.Message);
}
at initialization of the winform but it didn't work.
My problem is, if it was a Button or a Textfield, i could easily catch the exceptions in the event methods, but i have no idea about handling an exception that thrown by a PropertyGrid.
Any opinions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在转换(赋值)时抛出异常(即当 TypeConverter.ConvertFrom() 时) 被调用)到属性网格中的属性(例如,InvalidCastException),然后您可以在TypeConverter 。
您还可以重写基 TypeConverter 类的其他方法因此。
根据您添加的示例,您似乎希望推断与设备通信失败的原因(端口未打开等)。那么为什么不在
Device
类中设置一个属性或标志(例如,Error
或CommunicationError
)来告诉是否发生了特定错误。然后在访问其他属性之前,在
TypeConverter
中检查此属性,如果出错,则只需从ConvertFrom()
返回,而不调用base
实现。如果错误是可恢复的(在您的情况下似乎是通信错误),则允许用户重试,而不是抛出异常。此外,如果您将设备参数直接映射到类上,并且您有一个在属性网格中使用的属性,但不希望将其写入设备上,则使用自定义属性(例如 boolean
设备可写
)。在通信层(您实际将值写入设备的地方),检查此自定义属性,如果它设置为true
则写入设备,否则不写入设备。If the exception being thrown while converting (assigning value) (i.e. when TypeConverter.ConvertFrom() is called) to a property in the property grid (for example, InvalidCastException), then you can catch in the TypeConverter for that property.
You can also override other methods of the base TypeConverter class accordingly.
Going by the example, that you have added, it looks like you want the to deduce the reason why the communication with the device has failed (port not open, etc.). Then why not set a property or flag (for example,
Error
orCommunicationError
) in theDevice
class that tells whether the specific error has occurred.Then in the
TypeConverter
check for this property before accessing other properties, if in error then simply return from theConvertFrom()
without calling thebase
implementation. If the error is recoverable, which in your case appears to be a communication error, then allow the user to retry, rather than throwing an exception.In addition, if you are mapping the device parameters directly onto the class and you have a property that you use in the property grid but do not want it to be written on the device, then mark it with a custom attribute (say boolean
DeviceWritable
). In the communication layer (where you actually write the values to the device), check for this custom attribute, if it is set astrue
then write to the device otherwise do not write to the device.