OxyPlot在Xamarin Forms UWP中无法渲染

3

按照这个简单的例子,我在一个Xamarin Forms示例应用中尝试嵌入OxyPlot。我创建了一个全新的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);

感谢...

编辑:

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

1个回答

1
我遇到了你提到的同样问题,在使用Xamarin.Forms(3.1.0.697729)开发UWP时,饼图在Android上可以正常工作,但在UWP上无法工作。 最终,我通过安装来自此处的OxyPlot.Xamarin.Forms的最新版本(而不是1.0.0)使其在UWP上正常工作。它对于饼图很好用,但我没有测试其他绘图。
此外,Oxyplot文档此处指出,“必须设置VerticalOptions和HorizontalOptions属性”,但您还需要设置HeightRequest和WidthRequest。我在这个帖子中读到了这一点,但与此帖子相反,它在StackLayout中为我正常工作。 虽然这个问题很旧,但我希望这能有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接