Xamarin.Forms:不出现 OxyPlot 饼图 [英] Xamarin.Forms: Not appearing OxyPlot Pie Chart

查看:32
本文介绍了Xamarin.Forms:不出现 OxyPlot 饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我正在创建一个 Xamarin.Forms 便携式应用程序,我想使用 OxyPlot 在那里显示一个图表.

我遵循了 这里关于OxyPlot.但我还是有点糊涂.你能看看我遵循的过程并检查我所做的是否正确吗?这些只是我个人的理解.

这是我所做的:

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

    公共应用程序(){this.MainPage = 新的 ContentPage{内容 = 新的 PlotView{模型 = 新的 PlotModel { 标题 = 你好,表单!"},VerticalOptions = LayoutOptions.Fill,Horizo​​ntalOptions = LayoutOptions.Fill,},};}

  5. 在我的 XAML 页面中,我添加了这个命名空间声明:

<块引用>

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

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

     <oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" Horizo​​ntalOptions="Center"/>

    这是整个SalesPage.xaml的代码

    <ContentPage.BindingContext><ViewModels:SalesVM/></ContentPage.BindingContext><oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" Horizo​​ntalOptions="Center"/></内容页>

  2. 之后,我尝试调用包含名为 PiewViewModel.cs

    的图表内容的视图模型

    使用OxyPlot;使用 OxyPlot.Series;命名空间 ExampleLibrary{公共类 PieViewModel{私人 PlotModel 模型 P1;公共 PieViewModel(){modelP1 = new PlotModel { Title = "Pie Sample1" };动态系列P1 = 新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);}公共 PlotModel Model1{得到 { 返回模型 P1;}设置 { modelP1 = 值;}}}}

  3. 这是我的 Android 版 MainActivity.cs

     使用系统;使用 Android.App;使用 Android.Content.PM;使用 Android.Runtime;使用 Android.Views;使用 Android.Widget;使用Android.OS;使用 ImageCircle.Forms.Plugin.Droid;命名空间 XamarinFormsDemo.Droid{[活动(标签 =XamarinFormsDemo",图标 =@dr​​awable/recordsicon",MainLauncher = true,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]公共类 MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity{protected override void OnCreate(Bundle bundle){base.OnCreate(捆绑);global::Xamarin.Forms.Forms.Init(this, bundle);OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();加载应用程序(新应用程序());ImageCircleRenderer.Init();}}}

你能告诉我我做错了什么吗?图表没有出现.对像我这样的新手的任何帮助都非常感谢.非常感谢.

解决方案

你的 MainActivity 类需要派生自 AndroidActivity 而不是 FormsApplicationActivity

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.

解决方案

Your MainActivity class needs to derive from AndroidActivity not FormsApplicationActivity

这篇关于Xamarin.Forms:不出现 OxyPlot 饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆