【发布时间】:2016-11-25 20:15:23
【问题描述】:
大家好。我正在创建一个 Xamarin.Forms 便携式应用程序,我想在那里使用 OxyPlot 显示一个图表。
我关注了here 中关于 OxyPlot 的文档。但我还是有点困惑。您能否看看我遵循的过程并检查我所做的是否正确?这些只是基于我自己的理解。
这就是我所做的:
- 将 Xamarin.Forms NuGet 包更新到最新版本。
- 在可移植项目和特定平台项目中添加 OxyPlot.Xamarin.Forms NuGet 包。
- 我通过在 MainActivity.cs 中的 Xamarin.Forms.Forms.Init() 之后添加 OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init(); 来初始化 OxyPlot 渲染器
-
在便携式应用程序项目中,我将此绘图视图添加到我的 App.cs。
public App() { this.MainPage = new ContentPage { Content = new PlotView { Model = new PlotModel { Title = "Hello, Forms!" }, VerticalOptions = LayoutOptions.Fill, HorizontalOptions = LayoutOptions.Fill, }, }; } 在我的 XAML 页面中,我添加了这个命名空间声明:
xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
-
然后我在同一页面上添加了这个。
<oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" HorizontalOptions="Center" />这是整个 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" xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms" xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo" x:Class="XamarinFormsDemo.Views.SalesPage" BackgroundImage="bg3.jpg" Title="Sales Page"> <ContentPage.BindingContext> <ViewModels:SalesVM/> </ContentPage.BindingContext> <oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" HorizontalOptions="Center"/> </ContentPage> -
之后,我尝试调用包含图表内容的视图模型,名为 PiewViewModel.cs
using OxyPlot; using OxyPlot.Series; namespace ExampleLibrary { public class PieViewModel { private PlotModel modelP1; public PieViewModel() { modelP1 = new PlotModel { Title = "Pie Sample1" }; dynamic seriesP1 = new PieSeries { StrokeThickness = 2.0, InsideLabelPosition = 0.8, AngleSpan = 360, StartAngle = 0 }; seriesP1.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false, Fill = OxyColors.PaleVioletRed }); seriesP1.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true }); seriesP1.Slices.Add(new PieSlice("Asia", 4157) { IsExploded = true }); seriesP1.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true }); seriesP1.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = true }); modelP1.Series.Add(seriesP1); } public PlotModel Model1 { get { return modelP1; } set { modelP1 = value; } } } } -
这是我的 Android 版 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); OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init(); LoadApplication(new App()); ImageCircleRenderer.Init(); } } }
你能告诉我我做错了什么吗?图表没有出现。对像我这样的新手的任何帮助都非常感谢。非常感谢。
【问题讨论】:
-
那么是没有显示 PieGraph 的结果吗?还是有像运行时创建的 PlotView 一样的
Title = "Hello, Forms!"并且您希望看到Title = "Pie Sample1"根据设计时创建的 PlotView 及其关联的 PieViewModel 绑定? -
@JeremyThompson 先生,屏幕上什么都没有。
-
你能告诉我们你的
public class MainActivity : AndroidActivity例如:github.com/oxyplot/documentation-examples/blob/master/… -
@JeremyThompson 先生,我已经在我的代码中添加了它。谢谢
标签: c# xamarin xamarin.forms oxyplot