I3CHostInterface.cs的GetDeviceList返回值无法传递到AnalyzerViewModel.cs问题求助
I3CHostInterface到AnalyzerViewModel传值问题解决方案
核心故障原因排查
- 方法调用路径错误:
GetDeviceList是定义在I3CHostInterface类下的静态方法,你当前代码中直接调用HostInterface.GetDeviceList属于类名引用错误,无法正确触发目标方法 - 静态实例未初始化:仅声明了
public static HostInterface _instance;未做赋值操作,如果DeviceConfiguration.GetNodeList依赖该实例的初始化状态,会导致返回结果为空 - 命名空间缺失:AnalyzerViewModel.cs未引用I3CHostInterface所在的命名空间,导致类和方法无法被正确识别
- 底层方法返回空:
DeviceConfiguration.GetNodeList内部逻辑异常,没有返回符合要求的节点数组
修正代码
I3CHostInterface.cs 调整后代码
public class I3CHostInterface { // 初始化静态实例,避免依赖实例的逻辑异常 public static HostInterface _instance = new HostInterface(); public static NodeStructure[] GetDeviceList(eNodeType nodeType) { // 新增空返回兼容,避免上层调用报错 return DeviceConfiguration.GetNodeList(nodeType) ?? Array.Empty<NodeStructure>(); } }
AnalyzerViewModel.cs 调整后代码
// 顶部先引入I3CHostInterface所在的命名空间,示例: // using YourProjectName.InterfaceFolder; var traceDevice = new TraceConfigurations(); traceDevice.NodeList = new List<NodeStructure>(); // 修正类名,用I3CHostInterface调用静态方法 var masterDeviceList = I3CHostInterface.GetDeviceList(eNodeType.Master); // 新增非空判断,避免AddRange传入空值报错 if (masterDeviceList.Length > 0) { traceDevice.NodeList.AddRange(masterDeviceList); }
调试验证步骤
- 给
I3CHostInterface.GetDeviceList方法打断点,确认方法被正常触发,返回的数组长度符合预期 - 给ViewModel中
masterDeviceList赋值行打断点,确认返回值正常传递到ViewModel层 - 若返回值为空,排查
DeviceConfiguration.GetNodeList内部逻辑,确认节点配置数据是否正常加载
内容的提问来源于stack exchange,提问作者falcon
相关产品推荐
相关产品推荐

