使用POS .NET打开现金抽屉有投射错误?

时间:2014-09-09 15:01:24

标签: c#

我正在尝试使用POS .NET 1.14打开现金抽屉。经过一些研究,我在SO上找到了一些代码,但在尝试运行该示例时,我在Visual Studio中遇到错误:

  

错误1无法隐式转换类型' Microsoft.PointOfService.PosDevice'到' Microsoft.PointOfService.CashDrawer'。存在显式转换(您是否错过了演员?)

代码如下:

namespace POSDrawer
{
    public class CashDrawerClass
    {
        CashDrawer myCashDrawer;
        PosExplorer explorer;

        public CashDrawerClass()
        {
            explorer = new PosExplorer();
            DeviceInfo ObjDevicesInfo = explorer.GetDevice("CashDrawer");
            myCashDrawer = explorer.CreateInstance(ObjDevicesInfo); // THIS IS WHERE ERROR OCCURS
        }

        public void OpenCashDrawer()
        {
            myCashDrawer.Open();
            myCashDrawer.Claim(1000);
            myCashDrawer.DeviceEnabled = true;
            myCashDrawer.OpenDrawer();
            myCashDrawer.DeviceEnabled = false;
            myCashDrawer.Release();
            myCashDrawer.Close();
        }
    }
}

我检查了文档,而CreateInstance期待一个DeviceInfo,所以我不完全确定它为什么会抱怨?

MSDN documentation for PosExplorer.CreateInstance()

1 个答案:

答案 0 :(得分:2)

  

... CreateInstance期待一个DeviceInfo ...

那很好,但CreateInstance会返回什么?

您要将返回值分配给CashDrawer类型的字段,因此如果返回值不是CashDrawer(但是,例如,PosDevice),则需要先把它扔掉。