Field initializer needed 解决方法 cannot reference the non-static field method or 属性 in Blazor
Solution needed for Field initializer cannot reference the non-static field method or property in Blazor
我正在处理 Blazor 项目,但我的问题是 C#。我确实理解这个问题,但我无法在 Blazor 上下文中解决它。
在我的剃须刀页面中,我使用了一个以日期为 x 轴的 blazorise 折线图。如果我使用非动态日期,那效果非常好。
<LineChart @ref="lineChart" TItem="double" OptionsObject="@options"/>
但是,要动态确定日期(因此 x 轴标签),我需要在后面添加以下代码:
public partial class ChartComponent : ComponentBase
{
[Parameter]
public List<Observation> Observations { get; set; } //an Observation has a field with a Date in it.
Object options = new
{
Scales = new
{
XAxes = new[]
{
new
{
labels = new[]{GetDateTimeLabels()} //this gets me the error, if I use new[]{DateTime.Now,...,DateTime.Now+n} it works just fine.
}
}
}
};
public DateTime[] GetDateTimeLabels()
{
DateTime[] dateLabels = VACRAObservations.Select(x => x.Date).ToArray();
return dateLabels;
}
这让我得到一个错误“字段初始值设定项无法引用非静态字段方法或 属性”,据我所知,因为编译器不知道 GetLabels() 是否在对象选项之前准备就绪已初始化。
我无法绕过 [Parameter],我需要它来将 Observations 列表放入 razor 页面。
我试过都放在构造函数里面,但是构造函数在传入参数之前就已经初始化了
我尝试用静态来做到这一点,但是静态 class 不能从 Componentbase class.
派生
我不知所措。如果您需要更多信息,请告诉我。
最简单的修复方法是将其设为 属性。当你只需要它一次时,添加一个 >
:
Object options => new { ... };
以及当您需要防止过于频繁地评估它时:
Object _options = null;
Object options => _options ?? (_options = new
{
... // as before
});
感谢 Henk en enet:
<LineChart @ref="lineChart" TItem="double" OptionsObject="@Options"/>
这是工作部分class
public partial class xxxComponent : ComponentBase
{
public void Refresh()
{
StateHasChanged();
}
[Parameter]
public List<Observation> Observations { get; set; }
Object Options =>
new
{
Scales = new
{
XAxes = new[]
{
new
{
labels = GetDateTimeLabels() //changed to avoid a 2 dimensional array
}
}
}
};
LineChart<double> lineChart;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await HandleRedraw();
}
}
async Task HandleRedraw()
{
await lineChart.Clear();
await lineChart.AddDataSet(GetLineChartDataset());
await lineChart.Update();
}
LineChartDataset<double> GetLineChartDataset()
{
return new LineChartDataset<double>
{
Label = "VA",
Data = GetVALevels(),
Fill = false,
PointRadius = 2,
BorderDash = new List<int> { },
};
}
public DateTime[] GetDateTimeLabels()
{
DateTime[] dateLabels = Observations.Select(x => x.Date).ToArray();
return dateLabels;
}
public List<double> GetVALevels()
{
List<double> vALevels = Observations.Select(x => x.VA).ToList();
return vALevels;
}
}```
我正在处理 Blazor 项目,但我的问题是 C#。我确实理解这个问题,但我无法在 Blazor 上下文中解决它。
在我的剃须刀页面中,我使用了一个以日期为 x 轴的 blazorise 折线图。如果我使用非动态日期,那效果非常好。
<LineChart @ref="lineChart" TItem="double" OptionsObject="@options"/>
但是,要动态确定日期(因此 x 轴标签),我需要在后面添加以下代码:
public partial class ChartComponent : ComponentBase
{
[Parameter]
public List<Observation> Observations { get; set; } //an Observation has a field with a Date in it.
Object options = new
{
Scales = new
{
XAxes = new[]
{
new
{
labels = new[]{GetDateTimeLabels()} //this gets me the error, if I use new[]{DateTime.Now,...,DateTime.Now+n} it works just fine.
}
}
}
};
public DateTime[] GetDateTimeLabels()
{
DateTime[] dateLabels = VACRAObservations.Select(x => x.Date).ToArray();
return dateLabels;
}
这让我得到一个错误“字段初始值设定项无法引用非静态字段方法或 属性”,据我所知,因为编译器不知道 GetLabels() 是否在对象选项之前准备就绪已初始化。
我无法绕过 [Parameter],我需要它来将 Observations 列表放入 razor 页面。
我试过都放在构造函数里面,但是构造函数在传入参数之前就已经初始化了
我尝试用静态来做到这一点,但是静态 class 不能从 Componentbase class.
派生我不知所措。如果您需要更多信息,请告诉我。
最简单的修复方法是将其设为 属性。当你只需要它一次时,添加一个 >
:
Object options => new { ... };
以及当您需要防止过于频繁地评估它时:
Object _options = null;
Object options => _options ?? (_options = new
{
... // as before
});
感谢 Henk en enet:
<LineChart @ref="lineChart" TItem="double" OptionsObject="@Options"/>
这是工作部分class
public partial class xxxComponent : ComponentBase
{
public void Refresh()
{
StateHasChanged();
}
[Parameter]
public List<Observation> Observations { get; set; }
Object Options =>
new
{
Scales = new
{
XAxes = new[]
{
new
{
labels = GetDateTimeLabels() //changed to avoid a 2 dimensional array
}
}
}
};
LineChart<double> lineChart;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await HandleRedraw();
}
}
async Task HandleRedraw()
{
await lineChart.Clear();
await lineChart.AddDataSet(GetLineChartDataset());
await lineChart.Update();
}
LineChartDataset<double> GetLineChartDataset()
{
return new LineChartDataset<double>
{
Label = "VA",
Data = GetVALevels(),
Fill = false,
PointRadius = 2,
BorderDash = new List<int> { },
};
}
public DateTime[] GetDateTimeLabels()
{
DateTime[] dateLabels = Observations.Select(x => x.Date).ToArray();
return dateLabels;
}
public List<double> GetVALevels()
{
List<double> vALevels = Observations.Select(x => x.VA).ToList();
return vALevels;
}
}```