I have a large public class which called Telescope
. I have created a new form (form1.cs
) and I want to call some of the public void that the class has.
我有一个叫做望远镜的大型公共课。我创建了一个新表单(form1.cs),我想调用该类所具有的一些公共void。
I do something like this in the form to initialise the class
我在表单中做了类似的事情来初始化类
Telescope controls = new Telescope(); controls.CommandString("Gs#",true);
望远镜控制=新望远镜(); controls.CommandString( “GS#”,真正的);
After that I can see the all the methods but it fails in the execution as the class is already initialized and there is an existing serial port connection ongoing, so it reports that there is no serial port connection.
之后我可以看到所有方法,但它在执行失败,因为类已经初始化并且现有的串行端口连接正在进行,因此它报告没有串行端口连接。
Any help? How can I use the existing methods from the new form?
有帮助吗?如何使用新表单中的现有方法?
Telescope class is in Driver.cs
望远镜类在Driver.cs中
public string CommandString(string command, bool raw)
{
CheckConnected("CommandString");
serialPort.ClearBuffers();
serialPort.Transmit(command);
return serialPort.ReceiveTerminated("#");
}
When I use the CommandString in the Driver.cs (where the telescope class is) it works. It does not work from the form1.cs
当我使用Driver.cs中的CommandString(望远镜类所在的位置)时,它可以工作。它不适用于form1.cs
I get an exception:
我得到一个例外:
************** Exception Text **************
ASCOM.NotConnectedException: CommandString
2 个解决方案
#1
0
Ideally you would be using some sort of IoC container and your class would implement an interface containing the bare minimal methods to interface with your serial connection. The IoC container would then manage the lifetime of the instance as a singleton and on every request to resolve the interface would pass you back the existing instance.
理想情况下,您将使用某种IoC容器,您的类将实现一个包含与您的串行连接接口的极小方法的接口。然后,IoC容器将实例的生命周期作为单例进行管理,并且在每个解析接口的请求上都会将您传回现有实例。
Since this may not be the case and since only a single instance can access the serial port, you could move these methods into a static class... But be careful, when you start sharing static methods, unexpected bugs can arise. Depending on how the code is structured, you would want only the serial connection to be static.
由于情况可能并非如此,并且由于只有一个实例可以访问串行端口,因此可以将这些方法移动到静态类中......但是请注意,当您开始共享静态方法时,可能会出现意外错误。根据代码的结构,您只希望串行连接是静态的。
One example of how this could be implemented in a class:
如何在类中实现它的一个示例:
private Lazy<SerialConnection> _serialConnection =new Lazy<SerialConnection>(StaticClass.GetStaticSerialConnection);
public SerialConnection MySerialConnection
{
get { return _serialConnection.Value; }
}
#2
0
You should keep the reference to your first (and only) instance of Telescope class somewhere in your app and then access it by that reference. The reference could be kept in some static class, you could initialize it there and always call it using that static class.
您应该在应用程序的某处保留对Telescope类的第一个(也是唯一的)实例的引用,然后通过该引用访问它。引用可以保存在某个静态类中,您可以在那里初始化它并始终使用该静态类调用它。
#1
0
Ideally you would be using some sort of IoC container and your class would implement an interface containing the bare minimal methods to interface with your serial connection. The IoC container would then manage the lifetime of the instance as a singleton and on every request to resolve the interface would pass you back the existing instance.
理想情况下,您将使用某种IoC容器,您的类将实现一个包含与您的串行连接接口的极小方法的接口。然后,IoC容器将实例的生命周期作为单例进行管理,并且在每个解析接口的请求上都会将您传回现有实例。
Since this may not be the case and since only a single instance can access the serial port, you could move these methods into a static class... But be careful, when you start sharing static methods, unexpected bugs can arise. Depending on how the code is structured, you would want only the serial connection to be static.
由于情况可能并非如此,并且由于只有一个实例可以访问串行端口,因此可以将这些方法移动到静态类中......但是请注意,当您开始共享静态方法时,可能会出现意外错误。根据代码的结构,您只希望串行连接是静态的。
One example of how this could be implemented in a class:
如何在类中实现它的一个示例:
private Lazy<SerialConnection> _serialConnection =new Lazy<SerialConnection>(StaticClass.GetStaticSerialConnection);
public SerialConnection MySerialConnection
{
get { return _serialConnection.Value; }
}
#2
0
You should keep the reference to your first (and only) instance of Telescope class somewhere in your app and then access it by that reference. The reference could be kept in some static class, you could initialize it there and always call it using that static class.
您应该在应用程序的某处保留对Telescope类的第一个(也是唯一的)实例的引用,然后通过该引用访问它。引用可以保存在某个静态类中,您可以在那里初始化它并始终使用该静态类调用它。