我试着用MicroChart制作一个图表,通过LearnTechnologies youtube教程(https://www.youtube.com/watch?v=O6xhZkJa7H4&t=1s),但对我来说它不工作。我试着完全基于视频,但它仍然不工作。在模拟器中的图表不显示。我认为问题是与绑定,MainPage xaml指示{绑定LineChart} "没有为绑定'LineChart'找到DataContext。我的代码:
namespace MicroChartBinding.ViewModels
{
public class ViewModelBase : BindableBase, IInitialize, INavigationAware, IDestructible
{
protected INavigationService NavigationService { get; private set; }
private string _title;
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public ViewModelBase(INavigationService navigationService)
{
NavigationService = navigationService;
}
public virtual void Initialize(INavigationParameters parameters)
{
}
public virtual void OnNavigatedFrom(INavigationParameters parameters)
{
}
public virtual void OnNavigatedTo(INavigationParameters parameters)
{
}
public virtual void Destroy()
{
}
}
}
namespace MicroChartBinding.ViewModels
{
class MainPageViewModel : ViewModelBase
{
private LineChart lineChart;
public LineChart LineChart
{
get => lineChart;
set => SetProperty(ref lineChart, value);
}
private string[] months = new string[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
private float[] turnoverData = new float[] { 6010, 1000, 2524, 3245, 1245, 5767, 5544, 5242, 500, 1555, 400, 100 };
private SKColor blueColor = SKColor.Parse("#09C");
public MainPageViewModel(INavigationService navigationService)
: base(navigationService)
{
Title = "Main Page";
}
public override void Initialize(INavigationParameters parameters)
{
base.Initialize(parameters);
InitData();
}
private void InitData()
{
var turnoverEntries = new List<ChartEntry>();
foreach (var data in turnoverData)
{
turnoverEntries.Add(new ChartEntry(data)
{
Color = blueColor,
ValueLabel = $"{data / 1000} k",
Label = "trnover"
});
}
LineChart = new LineChart { Entries = turnoverEntries, LabelTextSize = 30f, LabelOrientation = Orientation.Horizontal };
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Charts="clr-namespace:Microcharts.Forms;assembly=Microcharts.Forms"
x:Class="MicroChartBinding.MainPage"
Title="{Binding Title}">
<Grid RowDefinitions="300,300,300">
<Charts:ChartView Chart ="{Binding LineChart}"
HeightRequest="300"
VerticalOptions="Start"
HorizontalOptions="Fill"/>
</Grid>
</ContentPage>
1条答案
按热度按时间5lhxktic1#
您共享的教程使用了nuget
Prism.Unity.Forms
,因此您需要如下修改代码App.xaml.cs
和App.xaml
,它们实现了ViewModel和页面的绑定。并在Android平台中修改代码,如下所示:
您可以获得上面教程here的示例。