Home > Mobile >  how to use object class instance recived as command parameters in WPF
how to use object class instance recived as command parameters in WPF

Time:02-01

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. but if i do dev.DeviceName this is giving error because dev object is recieving data on runtime. how can i use this Dev object and get data on runtime

CodePudding user response:

Assuming that Dev is actually a fixed type then you could try to define a Device class matching the properties.

  public void SelectTestCase(Device Dev)

and something like

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

    // other properties
}

CodePudding user response:

You have the parameter specifically as a generic "object" rather than the specific class type. 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.
}
  • Related