在Xamarin Forms Portable Application上不显示OxyPlot图表

时间:2016-07-23 09:30:45

标签: c# xamarin charts xamarin.forms oxyplot

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

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

PieViewModel.cs

var hi = new Hello(1)

SalesPage.XAML.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

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()
            {

            }


        }
    }

。 的 MainActivity.cs

<?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>

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

1 个答案:

答案 0 :(得分:3)

以下是一些示例代码:

应用:(便携式)

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

<强>视图模型:

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 :( Droid)

 [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