Xamarin.Forms: Not appearing OxyPlot Pie Chart

邮差的信 提交于 2021-02-08 08:21:19

问题


Good Day everyone. I'm creating a Xamarin.Forms Portable Application and I want to display there a Chart using a OxyPlot.

I followed a documentation in here about OxyPlot. But I still kinda confuse. Can you please look at the process I followed and check if what I did was right? These are just based on my own understanding.

Here's what I did :

  1. Update the Xamarin.Forms NuGet packages to the latest version.
  2. Add the OxyPlot.Xamarin.Forms NuGet package in both the portable and platform specific projects.
  3. I initialize the OxyPlot renderers by adding OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init(); just after the Xamarin.Forms.Forms.Init() in my MainActivity.cs
  4. In the portable app project, I added this plot view to my App.cs.

    public App()
    {
        this.MainPage = new ContentPage
        {
        Content = new PlotView
        {
        Model = new PlotModel { Title = "Hello, Forms!" },
        VerticalOptions = LayoutOptions.Fill,
        HorizontalOptions = LayoutOptions.Fill,
            },
        };
    }
    
  5. In my XAML page, I added this namespace declaration :

xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"

  1. Then I added this on the same page.

     <oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" HorizontalOptions="Center" />
    

    Here's the code of the whole 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>
    
  2. After that, I tried to call my view model that contains the content of my chart named 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; }
        }
       }
     }
    
  3. Here's my MainActivity.cs for Android

      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();
              }
          }
      }
    

Can you please tell me what I've done wrong? The chart isn't appearing. Any help to a newbie like me is highly appreciated. Thanks a lot.


回答1:


Your MainActivity class needs to derive from AndroidActivity not FormsApplicationActivity



来源:https://stackoverflow.com/questions/38517748/xamarin-forms-not-appearing-oxyplot-pie-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!