按照这个简单的例子,我在一个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);
感谢...
编辑:
我刚刚创建了一个新的测试项目,并得到了完全相同的结果,所以这对我来说不是特定于项目或机器...