c# - Xamarin Forms 可移植应用程序上未显示 OxyPlot 图表

标签 c# xamarin charts xamarin.forms oxyplot

我正在我的 Xamarin.Forms(可移植)应用程序中使用 OxyPlot 创建饼图。我创建了一个名为 PieViewModel 的 ViewModel,我在其中声明了饼图的内容。在我的 SalesPage.XAML.cs 中,我调用 ViewModel 并访问其中的 salesmodel。在我的 XAML 代码中,我将 salesmodel 绑定(bind)到我的代码中。

但是,我无法显示饼图。以下是我使用的代码:

PieViewModel.cs

using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

    namespace XamarinFormsDemo.ViewModels
    {
        public class PieViewModel : INotifyPropertyChanged
        {
            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; }
            }


            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

        }
    }

.

SalesPage.XAML.cs

using OxyPlot;
using OxyPlot.Xamarin.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.ViewModels;



using Xamarin.Forms;

    namespace XamarinFormsDemo.Views
    {
        public partial class SalesPage : ContentPage
        {

            public SalesPage()
            {

            }


        }
    }

. 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">



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

</ContentPage>

. 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;
using Xamarin.Forms.Platform.Android;

namespace XamarinFormsDemo.Droid
{
    [Activity(Label = "XamarinFormsDemo", Icon = "@drawable/recordsicon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : AndroidActivity
    {
        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();
        }
    }
}

请帮我解决这个问题。我真的对事情的进展感到困惑。多谢。

最佳答案

下面是一些示例代码:

应用:(可移植)

    public class App : Application
    {
        public App()
        {
            MainPage = new Page3();
        }
    }

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"
             x:Class="App26.Page3">
  <ContentPage.Content>
    <oxy:PlotView Model="{Binding MyModel}"></oxy:PlotView>
  </ContentPage.Content>
</ContentPage>

CS:

public partial class Page3 : ContentPage
{
    public MyViewModel vm { get; set; }

    public Page3()
    {
        InitializeComponent();

        vm = new MyViewModel();

        BindingContext = vm;
    }
}

View 模型:

public class MyViewModel
{
    public PlotModel MyModel { get; set; }

    public MyViewModel()
    {
        PieSeries pieSeries = new PieSeries();
        pieSeries.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false, Fill = OxyColors.PaleVioletRed });
        pieSeries.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Asia", 4157) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Oceania", 350) { IsExploded = true });

        MyModel = new PlotModel();
        MyModel.Series.Add(pieSeries);
    }
}

MainActivity:(机器人)

 [Activity(Label = "App26", Icon = "@drawable/icon", 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());
    }
}

enter image description here

关于c# - Xamarin Forms 可移植应用程序上未显示 OxyPlot 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540254/

相关文章:

c# - 转换没有时区的日期时间

animation - 如何在 Xamarin.Forms 中为不透明度设置动画

c# - "Red X of Doom"在WinForm中绘制多个时间序列图表

javascript - 使用按钮更改 Google 图表数据和外观

c# - 通过 C# 进行视频 session

c# - 多处理器和性能

c# - 是否可以使用自签名证书在不同机器上进行测试?

android - 膨胀类 android.suppot.design.widget.NavigationView 时出错

azure - Xamarin 通知推送 TodoItemManager

python: cairoplot gantt_chart 渲染问题