【问题标题】:"(object name) does not exist in the current context" when the item is clearly in scope?当项目明确在范围内时,“(对象名称)在当前上下文中不存在”?
【发布时间】:2021-07-08 22:46:19
【问题描述】:

我正在构建一个 Blazor 服务器端应用程序,我遇到了一个看似微不足道的问题,但我不知道出了什么问题。我的 ViewModels 文件夹中有一个名为 TestMonitorDUT.cs 的类,以及一个名为 TestMonitor.razor 的页面。在 razor 页面中,我有以下几行,我只是在其中创建了一个新的 TestMonitorDUT 并尝试调用一个成员函数:

@code {
TestMonitorDUT DUT = new TestMonitorDUT("1");
DUT.PopulateDictionaries();

在这种情况下,我收到错误消息: “当前上下文中不存在名称‘DUT’。”

我很困惑,因为 DUT 就在那里明确定义。感谢您的宝贵时间

【问题讨论】:

    标签: c# blazor


    【解决方案1】:

    转译为:

    class X
    {
      TestMonitorDUT DUT = new TestMonitorDUT("1");
      DUT.PopulateDictionaries();
    }
    

    DUT.PopulateDictionaries(); 是一个只需要声明的语句。它应该在方法内部。喜欢

    @code {
      TestMonitorDUT DUT = new TestMonitorDUT("1");
      //DUT.PopulateDictionaries();
    
      overide void OnInitialized()
      {
        DUT.PopulateDictionaries();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2013-10-02
      • 2014-04-22
      相关资源
      最近更新 更多