问题描述
大家好.我正在创建一个 Xamarin.Forms(便携式)应用程序,我想使用 OxyPlot 创建一个图表.我已经尝试过这段代码,但它有一个错误指向我的LoadApplication(new App()); 我的 Xamarin.Android 部分中的 MainActivity.cs 说明
Good Day Everyone. I'm creating a Xamarin.Forms (Portable) Application and I want to create a Chart using OxyPlot. I have tried this code but it has an error that points to myLoadApplication(new App()); MainActivity.cs in my Xamarin.Android part stating that
System.NullReferenceException:对象引用未设置为对象的实例"
您认为这背后的原因是什么?
What do you think is the reason behind this?
这些是我拥有的一些代码:
These are some of the codes I have:
SalesPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinFormsDemo.Views.SalesPage"
xmlns:oxy="clr-namespace:OxyPlot.XamarinForms;assembly=OxyPlot.XamarinForms"
BackgroundImage="bg3.jpg"
Title="Sales Page">
<StackLayout>
<oxy:PlotView Model="{Binding OxyPlotModel}" VerticalOptions="Center" HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
SalesPage.xaml
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace XamarinFormsDemo.Views
{
public partial class SalesPage
{
public SalesPage()
{
InitializeComponent();
var plotModel = new PlotModel { Title = "OxyPlot Demo" };
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });
var series1 = new LineSeries
{
MarkerType = OxyPlot.MarkerType.Circle,
MarkerSize = 4,
MarkerStroke = OxyPlot.OxyColors.White
};
series1.Points.Add(new DataPoint(0.0, 6.0));
series1.Points.Add(new DataPoint(1.4, 2.1));
series1.Points.Add(new DataPoint(2.0, 4.2));
series1.Points.Add(new DataPoint(3.3, 2.3));
series1.Points.Add(new DataPoint(4.7, 7.4));
series1.Points.Add(new DataPoint(6.0, 6.2));
series1.Points.Add(new DataPoint(8.9, 8.9));
plotModel.Series.Add(series1);
}
}
}
MainActivity.cs
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using ImageCircle.Forms.Plugin.Droid;
namespace XamarinFormsDemo.Droid
{
[Activity(Label = "XamarinFormsDemo", Icon = "@drawable/recordsicon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
ImageCircleRenderer.Init();
}
}
}
推荐答案
不要在任何地方为页面设置 DataContext.至少不在您显示的代码中.因此,绑定到 OxyPlotModel
属性(似乎也不存在)将不起作用.
You don't set the DataContext for the page anywhere. At least not in the code that you are showing. Hence, the binding to the OxyPlotModel
property, which also doesn't seem to exist, will not work.
如果您想要快速而肮脏的修复只是为了查看您在页面中创建的模型,那么只需将其分配给 PlotView
,如下所示:
If you want a quick and dirty fix just to see the model you have made in your page, then simply assign it to the PlotView
like so:
- 向绘图视图添加名称属性.
x:Name="Graph"
- 只需将创建的 PlotModel 分配给
Graph
的 Model 属性:Graph.Model = plotModel
.
- Add a name attribute to your plotview.
x:Name="Graph"
- Simply assign the created PlotModel to the Model property of
Graph
:Graph.Model = plotModel
.
但是,您可能需要在 OnAppearing
覆盖中执行此操作,因此您的代码可能需要在该方法中向下移动,例如:
However, you might need to do that in an OnAppearing
override, so your code might need to be moved down in that method like:
public override void OnAppearing()
{
var plotModel = new PlotModel { Title = "OxyPlot Demo" };
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });
var series1 = new LineSeries
{
MarkerType = OxyPlot.MarkerType.Circle,
MarkerSize = 4,
MarkerStroke = OxyPlot.OxyColors.White
};
series1.Points.Add(new DataPoint(0.0, 6.0));
series1.Points.Add(new DataPoint(1.4, 2.1));
series1.Points.Add(new DataPoint(2.0, 4.2));
series1.Points.Add(new DataPoint(3.3, 2.3));
series1.Points.Add(new DataPoint(4.7, 7.4));
series1.Points.Add(new DataPoint(6.0, 6.2));
series1.Points.Add(new DataPoint(8.9, 8.9));
plotModel.Series.Add(series1);
Graph.Model = plotModel;
}
一个更好的方法是利用 MVVM 模式,通过像这样创建一个 ViewModel:
A better way to do this is to utilize the MVVM pattern, by creating a ViewModel like so:
public class SalesViewModel : INotifyPropertyChanged
{
private PlotModel _plotModel;
public PlotModel PlotModel
{
get { return _plotModel; }
set {
_plotModel = value;
RaisePropertyChanged();
}
}
// other methods here that create your model
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler (this, new PropertyChangedEventArgs (propertyName));
}
}
然后您可以在某处实例化您的 ViewModel,可能在 OnAppearing() 中并将其设置为 BindingContext:
Then you can instantiate your ViewModel somewhere, probably in OnAppearing() and set it as BindingContext:
public override void OnAppearing()
{
var viewModel = new SalesViewModel();
this.BindingContext = viewModel;
}
然后您可以使用您在页面中创建的绑定:
Then you can use that binding you created in your Page:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinFormsDemo.Views.SalesPage"
xmlns:oxy="clr-namespace:OxyPlot.XamarinForms;assembly=OxyPlot.XamarinForms"
BackgroundImage="bg3.jpg"
Title="Sales Page">
<StackLayout>
<oxy:PlotView Model="{Binding PlotModel}" VerticalOptions="Center" HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
记住在每次更改PlotModel
时调用RaisePropertyChanged()
以使其反映在视图中:RaisePropertyChanged("PlotModel");代码>
Remember to call RaisePropertyChanged()
on your PlotModel
every time you change it to get it reflected in the View: RaisePropertyChanged("PlotModel");
这篇关于Xamarin.Forms:使用 OxyPlot 创建图表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!