C# 如何访问另一个类中的属性?

C# 如何访问另一个类中的属性?,c#,wpf,mvvm,properties,C#,Wpf,Mvvm,Properties,我正在处理一个WPF.NETCore3项目 在我的UnbalancedViewModel中,我需要从另一个类(TestRunDto.cs)访问一个ID。 不平衡视图模型 public class UnbalancedViewModel : ViewModelBase, IUnbalancedViewModel { private TestRunApi _testRunApi; public UnbalancedViewModel(TestRunApi test

我正在处理一个WPF.NETCore3项目

在我的UnbalancedViewModel中,我需要从另一个类(TestRunDto.cs)访问一个ID。

不平衡视图模型

public class UnbalancedViewModel : ViewModelBase, IUnbalancedViewModel
{        
    private TestRunApi _testRunApi;

    public UnbalancedViewModel(TestRunApi testRunApi, INotificationManager notifications)
    {            
     _testRunApi = testRunApi; 
    }

    private void StartTestRunJobExecuted(object obj)
    {
        _testRunApi.StartTestRun(1); ////I need the Id from TestRunDto (TestRunDto.Id)
    }
}
TestRunApi

public async Task<TestRunLiveValueDto> GetTestRunLiveValue(int jobRunId)
{
    await using var dbContext = new AldebaDbContext(_connectionString);
    return await TestRunInteractor.GetTestRunLiveValue(jobRunId, dbContext);
}

public async Task StartTestRun(int testRunId)
{
    await using var dbContext = new AldebaDbContext(_connectionString);
    await TestRunInteractor.StartTestRun(dbContext, testRunId);
}
public异步任务getTestRunniveValue(int-jobRunId)
{
wait using var dbContext=new AldebaDbContext(_connectionString);
返回wait-wait-testruninteractitor.GetTestRunLiveValue(jobRunId,dbContext);
}
公共异步任务StartTestTrun(int testRunId)
{
wait using var dbContext=new AldebaDbContext(_connectionString);
等待TestRunInteractor.StartTestRun(dbContext,testRunId);
}
TestRunLiveValueDto

public class TestRunLiveValueDto
{
    public TestRunDto TestRun { get; }
    public bool ShowInstantaneousValue { get; set; }
    public bool EnableStart { get; set; }
    public bool EnableStop { get; set; }
    public bool EnableMeasure { get; set; }
    public int RecipeRpm { get; }
    public string ActualRecipeName { get; }
    public int DefaultSetOfPlaneId { get; }

    public ICollection<BalancePlaneDto> ListBalancePlane { get; }
    public ICollection<SetOfPlaneDto> ListSetOfPlane { get; }
    public ICollection<SensorVibrationDto> SensorVibrations { get; set; }
    public ICollection<EstimationDto> InstantaneousValues { get; set; }
    public ICollection<EstimationDto> EstimationsValues { get; set; }

    private TestRunLiveValueDto(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm,  ICollection<SensorVibrationDto> sensorVibrations)
    {
        EnableStart = enableStart;
        EnableStop = enableStop;
        TestRun = testRun;
        RecipeRpm = recipeRpm;
        SensorVibrations = sensorVibrations;
    }

    public static TestRunLiveValueDto Create(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm, ICollection<SensorVibrationDto> sensorVibrations)
        => new TestRunLiveValueDto(testRun,  enableStart,  enableStop, recipeRpm, sensorVibrations);
}
public class TestRunDto
{
    public int Id { get; set; }
    public int JobRunId { get; set; }
    public string Name { get; set; }
    public int TestRunNumber { get; set; }
    public RunState State { get; set; }

    public ICollection<BalancePlaneDto> BalancePlanes { get; set; } // Todo remove

    private TestRunDto(int id, int jobRunId, RunState state, string name, int testRunNumber)
    {
        Id = id;
        JobRunId = jobRunId;
        Name = name;
        TestRunNumber = testRunNumber;
        State = state;
    }

    public static TestRunDto Create(int id, int jobRunId, RunState state, string name, int testRunNumber)
        => new TestRunDto(id, jobRunId, state, name, testRunNumber);
}
公共类TestRunLiveValueDto
{
公共testrund到TestRun{get;}
公共bool showInstanceValue{get;set;}
public bool EnableStart{get;set;}
公共布尔启用停止{get;set;}
公共布尔启用度量{get;set;}
public int RecipeRpm{get;}
公共字符串ActualRecipeName{get;}
public int DefaultSetOfPlaneId{get;}
公共ICollection ListBalancePlane{get;}
公共ICollection ListSetOfPlane{get;}
公共ICollection{get;set;}
公共ICollection瞬时值{get;set;}
公共ICollection估计值{get;set;}
私有TestRunLiveValueDto(TestRunDto testRun、bool enableStart、bool enableStop、int recipeRpm、ICollection sensor)
{
EnableStart=EnableStart;
EnableStop=EnableStop;
TestRun=TestRun;
RecipeRpm=RecipeRpm;
传感器振动=传感器振动;
}
要创建的公共静态TestRunLiveValuedt(TestRundtoTestRun、bool enableStart、bool enableStop、int recipeRpm、ICollection sensor)
=>新的TestRunLiveValueDto(testRun、enableStart、enableStop、recipeRpm、Sensor振动);
}
TestRunDto

public class TestRunLiveValueDto
{
    public TestRunDto TestRun { get; }
    public bool ShowInstantaneousValue { get; set; }
    public bool EnableStart { get; set; }
    public bool EnableStop { get; set; }
    public bool EnableMeasure { get; set; }
    public int RecipeRpm { get; }
    public string ActualRecipeName { get; }
    public int DefaultSetOfPlaneId { get; }

    public ICollection<BalancePlaneDto> ListBalancePlane { get; }
    public ICollection<SetOfPlaneDto> ListSetOfPlane { get; }
    public ICollection<SensorVibrationDto> SensorVibrations { get; set; }
    public ICollection<EstimationDto> InstantaneousValues { get; set; }
    public ICollection<EstimationDto> EstimationsValues { get; set; }

    private TestRunLiveValueDto(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm,  ICollection<SensorVibrationDto> sensorVibrations)
    {
        EnableStart = enableStart;
        EnableStop = enableStop;
        TestRun = testRun;
        RecipeRpm = recipeRpm;
        SensorVibrations = sensorVibrations;
    }

    public static TestRunLiveValueDto Create(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm, ICollection<SensorVibrationDto> sensorVibrations)
        => new TestRunLiveValueDto(testRun,  enableStart,  enableStop, recipeRpm, sensorVibrations);
}
public class TestRunDto
{
    public int Id { get; set; }
    public int JobRunId { get; set; }
    public string Name { get; set; }
    public int TestRunNumber { get; set; }
    public RunState State { get; set; }

    public ICollection<BalancePlaneDto> BalancePlanes { get; set; } // Todo remove

    private TestRunDto(int id, int jobRunId, RunState state, string name, int testRunNumber)
    {
        Id = id;
        JobRunId = jobRunId;
        Name = name;
        TestRunNumber = testRunNumber;
        State = state;
    }

    public static TestRunDto Create(int id, int jobRunId, RunState state, string name, int testRunNumber)
        => new TestRunDto(id, jobRunId, state, name, testRunNumber);
}
公共类TestRunDto
{
公共int Id{get;set;}
public int JobRunId{get;set;}
公共字符串名称{get;set;}
public int TestRunNumber{get;set;}
公共运行状态状态{get;set;}
公共ICollection BalancePlanes{get;set;}//Todo remove
私有TestRunDto(int-id、int-jobRunId、RunState-state、字符串名、int-testRunNumber)
{
Id=Id;
JobRunId=JobRunId;
名称=名称;
TestRunNumber=TestRunNumber;
状态=状态;
}
要创建的公共静态testrundt(int-id、int-jobRunId、RunState-state、字符串名、int-testRunNumber)
=>新的TestRunDto(id、jobRunId、state、name、testRunNumber);
}

我一直试图理解这一点,但我无法掌握正确的方法来做到这一点。我是先在viewmodel中声明一个新的
TestRunDto
类,还是应该以其他方式访问它?

您需要确保类a引用类B的一个实例来访问属性,例如,一种方法是通过一种方法将类a传递给B,您可以在该方法中操作或访问属性

public class FooA 
{
   public string PropertyA { get; set; }
}

public class FooB 
{ 
   public string PropertyB { get; set; } 

   public void CanAccessFooA(FooA a) 
   { 
       a.PropertyA = "See, I can access this here";
   } 
}
另一种方法是在构造函数中将类A传递给B(称为依赖项注入)

具体如何构造代码取决于您,但原则不变:类B只有在引用了类A的实例时才能访问该类。

研究“依赖注入”,因为有很多技术可以实现这一点

编辑

一种这样的技术可能是抽象代码以向两者提供ID,如下所示

public class IdProvider 
{
    public int Id { get; set; } 
} 

public class FooA 
{
   private int _id; 
   public FooA(IdProvider idProvider) 
   {
      _id = idProvider.Id;
   }
} 

public class FooB
{
   private int _id; 
   public FooB(IdProvider idProvider) 
   {
      _id = idProvider.Id;
   }
} 

现在两个类都有相同的ID

您需要确保类A引用了类B的实例才能访问属性,例如,一种方法是在可以操作或访问属性的方法中将类A传递给B

public class FooA 
{
   public string PropertyA { get; set; }
}

public class FooB 
{ 
   public string PropertyB { get; set; } 

   public void CanAccessFooA(FooA a) 
   { 
       a.PropertyA = "See, I can access this here";
   } 
}
另一种方法是在构造函数中将类A传递给B(称为依赖项注入)

具体如何构造代码取决于您,但原则不变:类B只有在引用了类A的实例时才能访问该类。

研究“依赖注入”,因为有很多技术可以实现这一点

编辑

一种这样的技术可能是抽象代码以向两者提供ID,如下所示

public class IdProvider 
{
    public int Id { get; set; } 
} 

public class FooA 
{
   private int _id; 
   public FooA(IdProvider idProvider) 
   {
      _id = idProvider.Id;
   }
} 

public class FooB
{
   private int _id; 
   public FooB(IdProvider idProvider) 
   {
      _id = idProvider.Id;
   }
} 

现在两个类都有相同的ID

StartTestRun
tesRunId
作为参数

public async Task StartTestRun(int testRunId)
{
我想您需要使用这个
testRunId
调用
StartTestRunJobExecuted

你需要改变

private void StartTestRunJobExecuted(object obj)


(这是基于我的猜测)。

StartTestRun
tesRunId
作为参数

public async Task StartTestRun(int testRunId)
{
我想您需要使用这个
testRunId
调用
StartTestRunJobExecuted

你需要改变

private void StartTestRunJobExecuted(object obj)


(这是基于我的猜测)。

什么调用
StartTestRunJobExecuted
?它是通过单击按钮调用的-starttestrunjobecommand=newdelegatecommand(StartTestRunJobExecuted);什么调用
StartTestRunJobExecuted
?通过单击按钮调用-starttestrunjobecommand=newdelegatecommand(StartTestRunJobExecuted);