简体   繁体   English

如何使用 object class 实例作为 WPF 中的命令参数接收

[英]how to use object class instance recived as command parameters in WPF

I am passing command parameters to a command.我正在将命令参数传递给命令。 and receiving it like this并像这样接收

 public void SelectTestCase(object Dev)
    {
        try
        {               
            _navigationStore.CurrentViewModel = new TestCaseViewModel(_navigationStore);                
        }
        catch (Exception e)
        {
        }
    }

in this Object Dev will be carying data related to Device.在这个 Object Dev 中会携带 Device 相关的数据。 but if i do dev.DeviceName this is giving error because dev object is recieving data on runtime.但如果我这样做 dev.DeviceName 这会给出错误,因为 dev object 正在运行时接收数据。 how can i use this Dev object and get data on runtime我如何使用这个 Dev object 并在运行时获取数据

Assuming that Dev is actually a fixed type then you could try to define a Device class matching the properties.假设 Dev 实际上是固定类型,那么您可以尝试定义一个与属性匹配的 Device class。

  public void SelectTestCase(Device Dev)

and something like和类似的东西

public class Device
{
    public string DeviceName {get;set;}

    // other properties
}

You have the parameter specifically as a generic "object" rather than the specific class type.您将参数专门作为通用“对象”而不是特定的 class 类型。 You need to type-cast it.您需要对其进行类型转换。 Ex:前任:

public void SelectTestCase( object Dev )
{
   if( Dev is myDeviceTypeClass )
   {
      var tmp = (myDeviceClass)Dev;
      // Now you can use as needed
      MessageBox.Show( tmp.DeviceName );
   }
   
   // if you have different POSSIBLE device classes passed in,
   // just test for those too.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM