我一直在尝试将Microcharts集成到我的Xamarin项目中。当我希望它们存在于列表视图中时,我将它们添加到Model-View-Class中,并在此处实例化BarChart类:
public class ChangeOverview
{
Chart _SuccessChart = null;
public Chart SuccessChart { get; }
List<Entry> _entries = new List<Entry>()
{
new Entry(200)
{
Label = "January",
ValueLabel = "200",
Color = SKColor.Parse("#FF0033")
},
new Entry(300)
{
Label = "February",
ValueLabel = "300",
Color = SKColor.Parse("#FF0000")
},
new Entry(400)
{
Label = "March",
ValueLabel = "400",
Color = SKColor.Parse("#AA0000")
},
};
public List<Entry> ChartEntries
{
get
{
return _entries;
}
}
public ChangeOverview(Change c)
{
_SuccessChart = new BarChart()
{
Entries = _entries
};
}
}
因此,在我的ContentPage.xaml.cs中,我执行了以下操作:
public ChangeList ()
{
InitializeComponent ();
Debug.WriteLine("### Changes: " + User.LoggedUser.AllMyChanges.Count);
foreach (Change c in User.LoggedUser.AllMyChanges)
{
list.Add(new ChangeOverview(c));
}
TestChart.Chart = list[0].SuccessChart;
}
这应该很简单,但是并没有显示图表。当我四处搜索时,发现应该将代码放在OnAppearing方法中:
protected override void OnAppearing()
{
base.OnAppearing();
TestChart.Chart = list[0].SuccessChart;
TestChart.Chart = new BarChart() { Entries = list[0].ChartEntries };
}
仅分配图表对我没有帮助,我真的必须在此方法中实例化BarChart()。
还有另一种方法吗?我可以以某种方式刷新图表吗?还是我真的必须在OnAppearing方法中在此处实例化Chart?
谢谢!