【问题标题】:Xamarin.Forms: Not appearing OxyPlot Pie ChartXamarin.Forms:没有出现 OxyPlot 饼图
【发布时间】:2016-11-25 20:15:23
【问题描述】:

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

我关注了here 中关于 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。

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

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

【问题讨论】:

  • 那么是没有显示 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


【解决方案1】:

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

【讨论】:

  • @JeremyStephens 我更换了它,但什么也没发生,先生?你检查过我的代码吗?你认为我的绑定正确吗?感谢您回答先生。
  • MainActivity 类是唯一一个看起来与演示示例不同的类。为什么不下载示例并构建它以确认它有效,然后与它进行比较并发现差异。
  • 但这就是我所做的?不知道为什么没有出现?
猜你喜欢
  • 2017-01-07
  • 2016-11-22
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多