尝试在对象列表中使用非静态对象

时间:2019-04-26 13:35:05

标签: c# razor

我正在使用Razor组件,并且对在non-static values中使用list of objects有疑问。

我有以下代码:

     @page "/CircularGauge/DefaultFunctionalities"

@using Syncfusion.EJ2.RazorComponents.CircularGauge



   <div class="control-section">
      <EjsCircularGauge ID="gauge" Axes="axes"></EjsCircularGauge>
   </div>

   @functions {
   int myInteger = 21;
   public List<Object> axes { get; set; } = new List<Object>
   {
   new
   {
      radius = "80%",
      startAngle = 230,
      endAngle = 130,
      majorTicks = new { width = "0px" },
      minorTicks = new { width = "0px" },
      lineStyle = new { width = 8, color = "#E0E0E0" },
      labelStyle = new { font = new { fontFamily = "Roboto", size =      "12px", fontWeight = "Regular" }, offset = -5 },
      pointers  = new List<Object>
      {
      new
      {
        value = 60,
        radius = "60%",
        color = "#757575",
        pointerWidth = 7,
        cap = new { radius = 8, color = "#757575", border = new { width = 0 } },
        needleTail = new { color = "#757575", length = "25%" }
    }
}
 }
  };
  }

来源:https://ej2.syncfusion.com/aspnet-core-blazor/CircularGauge/DefaultFunctionalities

我在代码中使用一个整数,例如:

     int myInteger = 21;

我想做的是上面的代码,在:

      pointers  = new List<Object>
      {
      new
      {
        ****value = 60,****
        radius = "60%",
        color = "#757575",
        pointerWidth = 7,
        cap = new { radius = 8, color = "#757575", border = new { width = 0 } }
     }

正在将value的值更改为我的本地整数,但这是不可能的。

This is why

我一直在寻找解决方案。有没有对此有所了解的人?

1 个答案:

答案 0 :(得分:1)

您正在使用内联构造函数初始化属性。您无法访问内联构造函数中的实例字段,因为编译器无法确保首先完成哪个实例。

您应将myInteger定义为静态private static int myInteger。或者,您可以在构造函数中建立属性的值,然后可以在构造函数中使用局部变量。

但是,如果您想定义一个像这样的常量,那么我建议您更多地这样做:

private const int MY_INTEGER = 21;