Xamarin.窗体:不显示OxyPlot饼图

本文关键字:OxyPlot 饼图 显示 窗体 Xamarin | 更新日期: 2023-09-27 17:58:19

大家好。我正在创造一个Xamarin。Forms Portable Application和我想在那里显示一个使用OxyPlot的图表。

我在这里查阅了一份关于OxyPlot的文档。但我还是有点困惑。你能看看我遵循的流程并检查我所做的是否正确吗?这些只是基于我自己的理解。

以下是我所做的:

  1. 更新Xamarin。Forms NuGet包到最新版本
  2. 添加OxyPlot。Xamarin。Forms NuGet包在可移植项目和特定于平台的项目中
  3. 我通过添加OxyPlot来初始化OxyPlot渲染器。Xamarin。表格。站台安卓PlotViewRenderer。Init()就在Xamarin之后。表格。表格。Init()在我的MainActivity.cs中
  4. 在便携式应用程序项目中,我将此绘图视图添加到我的app.cs.中

    public App()
    {
        this.MainPage = new ContentPage
        {
        Content = new PlotView
        {
        Model = new PlotModel { Title = "Hello, Forms!" },
        VerticalOptions = LayoutOptions.Fill,
        HorizontalOptions = LayoutOptions.Fill,
            },
        };
    }
    
  5. 在我的XAML页面中,我添加了以下命名空间声明:

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

  1. 然后我在同一页上添加了这个。

     <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>
    
  2. 之后,我尝试将包含图表内容的视图模型调用为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. 这是我的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();
              }
          }
      }
    

你能告诉我我做错了什么吗?图表未显示。非常感谢对像我这样的新手的任何帮助。非常感谢。

Xamarin.窗体:不显示OxyPlot饼图

MainActivity类需要从AndroidActivity而不是FormsApplicationActivity 派生