C# 表单:使用OxyPlot创建图表时出错
大家好。我正在创建一个Xamarin.Forms(便携)应用程序,我想使用OxyPlot创建一个图表。我尝试过这段代码,但它有一个错误,指向我的 加载应用程序(新应用程序())MainActivity.cs在我的Xamarin.Android部分中声明 “System.NullReferenceException:对象引用未设置为对象的实例” 你认为这背后的原因是什么 以下是我的一些代码: SalesPage.xamlC# 表单:使用OxyPlot创建图表时出错,c#,xamarin,xamarin.forms,oxyplot,C#,Xamarin,Xamarin.forms,Oxyplot,大家好。我正在创建一个Xamarin.Forms(便携)应用程序,我想使用OxyPlot创建一个图表。我尝试过这段代码,但它有一个错误,指向我的 加载应用程序(新应用程序())MainActivity.cs在我的Xamarin.Android部分中声明 “System.NullReferenceException:对象引用未设置为对象的实例” 你认为这背后的原因是什么 以下是我的一些代码: SalesPage.xaml <?xml version="1.0" encoding="utf-8
<?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>
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
属性(该属性似乎不存在)将无法工作
如果您只想快速修复页面中的模型,只需将其分配到PlotView
,如下所示:
Graph
:Graph.Model=PlotModel
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:
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:
public override void OnAppearing()
{
var viewModel = new SalesViewModel();
this.BindingContext = viewModel;
}
然后,您可以使用在页面中创建的绑定:
<?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>
记得每次更改
RaisePropertyChanged()
绘图模型时,都要调用RaisePropertyChanged()
,以使其反映在视图中:RaisePropertyChanged(“绘图模型”)代码>您没有在任何地方设置页面的DataContext。至少在您显示的代码中没有。因此,绑定到OxyPlotModel
属性(该属性似乎不存在)将无法工作
如果您只想快速修复页面中的模型,只需将其分配到PlotView
,如下所示:
将名称属性添加到plotview<代码>x:Name=“图形”
Graph
:Graph.Model=PlotModel
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:
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:
public override void OnAppearing()
{
var viewModel = new SalesViewModel();
this.BindingContext = viewModel;
}
然后,您可以使用在页面中创建的绑定:
<?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>
记得每次更改
RaisePropertyChanged()
绘图模型时,都要调用RaisePropertyChanged()
,以使其反映在视图中:RaisePropertyChanged(“绘图模型”)代码>没错,先生,我不知道我复制的代码中的绘图模型在哪里。然而,我认为遵循您的代码可能会成功?我能把它分解一下吗?首先,在OnAppearing覆盖中,我应该在哪里复制您的代码?第二,我是否需要创建ViewModel?第三,如何在OnAppearingOverride中实例化ViewModel?最后,我应该在.XAML文件中放什么?非常感谢,先生。好的,先生。我只是有点困惑,因为我觉得代码不完整。如果你不介意的话,你能告诉我第一步和下一步要做什么吗?我应该开始编码XAML吗?然后呢?非常感谢,先生。给你更多的力量代码是不完整的,因为你必须用你自己的大脑来填充剩下的部分。没有人会给你一个完整的复制/粘贴解决方案,它可以完美地嵌入到你的代码中,并且直接开箱即用。我给了你一个非常好的例子,展示了两种不同的方法。如果你不知道如何使用这段代码,那就聪明一点,准确地描述一下你尝试过什么,出了什么问题。我猜不出你对所提供的例子做了什么,先生。很抱歉无论如何,谢谢你的帮助。上帝保佑,没错,先生,我不知道我复制的代码里的模型在哪里。然而,我认为遵循您的代码可能会成功?我能把它分解一下吗?首先,在OnAppearing覆盖中,我应该在哪里复制您的代码?第二,我是否需要创建ViewModel?第三,如何在OnAppearingOverride中实例化ViewModel?最后,我应该在.XAML文件中放什么?非常感谢,先生。好的,先生。我只是有点困惑,因为我觉得代码不完整。如果你不介意的话,你能告诉我第一步和下一步要做什么吗?我应该开始编码XAML吗?然后呢?非常感谢,先生。给你更多的力量代码是不完整的,因为你必须用你自己的大脑来填充剩下的部分。没有人会给你一个完整的复制/粘贴解决方案,它可以完美地嵌入到你的代码中,并且直接开箱即用。我给了你一个非常好的例子,展示了两种不同的方法。如果你不知道如何使用这段代码,那就聪明一点,准确地描述一下你尝试过什么,出了什么问题。我猜不出你对所提供的例子做了什么,先生。很抱歉无论如何,谢谢你的帮助。上帝保佑。