3

按照这个简单的示例,我只是在尝试将 OxyPlot 嵌入到 Xamarin Forms Sample 应用程序中。我创建了一个全新的 Xamarin Forms 应用程序,添加了 Nuget 包,并在每个项目中添加了 OxyPlot 所需的初始化代码。然后,在我的 MainPage.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:local="clr-namespace:OxyPlotTestApp"
             x:Class="OxyPlotTestApp.MainPage">

    <AbsoluteLayout>
        <oxy:PlotView Model="{Binding PieModel}" AbsoluteLayout.LayoutBounds="20,0,.9,.9" 
                      AbsoluteLayout.LayoutFlags="WidthProportional,HeightProportional" />
    </AbsoluteLayout>

</ContentPage>

在代码隐藏中:

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.Xamarin.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace OxyPlotTestApp
{
    public class PieViewModel
    {
        public PlotModel PieModel { get; set; }

        public PieViewModel()
        {
            PieModel = CreatePieChart();

        }

        private PlotModel CreatePieChart()
        {
            var model = new PlotModel { Title = "World Population By Content" };
            var ps = new PieSeries
            {
                StrokeThickness = .25,
                InsideLabelPosition = 0.25,
                AngleSpan = 360,
                StartAngle = 0
            };
            ps.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Americas", 929) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Asia", 4157));
            ps.Slices.Add(new PieSlice("Europe", 739) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = false });
            model.Series.Add(ps);
            return model;
        }

    }
    public partial class MainPage : ContentPage
    {
        public PieViewModel vm { get; set; }
        public MainPage()
        {
            InitializeComponent();
            vm = new PieViewModel();
            this.BindingContext = vm;
        }

    }
}

我不确定我错过了什么,如果有的话,但我已经尝试过 UWP 项目和 Android 项目(后者在物理设备上)并且它在 Android 上很好,但 UWP 应用程序只是呈现空白页。

我的 UWP App.xaml.cs(相关部分):

if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

                OxyPlot.Xamarin.Forms.Platform.UWP.PlotViewRenderer.Init();

                Xamarin.Forms.Forms.Init(e);

谢谢...

编辑:

我刚刚创建了一个新的测试项目并获得了完全相同的结果,所以它不是特定于我的项目或机器......

4

1 回答 1

1

I had the same problem as you mentioned, a PieChart working on Android but not on UWP with Xamarin.Forms (3.1.0.697729). I finally made it work on UWP by installing the latest version of OxyPlot.Xamarin.Forms from here instead of the 1.0.0. It works fine for a PieChart, I did not test other plots.

In addition, the Oxyplot documentation here says that "the VerticalOptions and HorizontalOptions properties must be set" but you also need to set the HeightRequest and WidthRequest. I read that in this post, but contrary to this post, it works fine in a StackLayout for me. The question is old but I hope it will help !

于 2019-01-29T10:34:44.383 回答