【发布时间】:2016-07-23 09:30:44
【问题描述】:
我正在我的 Xamarin.Forms(便携式)应用程序中使用 OxyPlot 创建一个饼图。我创建了一个名为 PieViewModel 的 ViewModel,在其中声明了饼图的内容。在我的 SalesPage.XAML.cs 中,我调用 ViewModel 并访问其中的 salesmodel。在我的 XAML 代码中,我将 salesmodel 绑定到我的代码中。
但是,我无法显示饼图。以下是我使用的代码:
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();
}
}
}
请帮我解决这个问题。我真的对事情的进展感到困惑。非常感谢。
【问题讨论】:
-
一方面,当您的
pvm和您的salesmodel应该是公共属性以使绑定起作用时,它们被声明为局部变量。 -
@jstreet 我应该做这样的事情吗? > 公共字符串销售模型 { 获取;放; } ?我应该使用什么数据类型?
-
根据您的代码,
salesmodel应该是PlotModel。 -
@jstreet 我把这个放在我的 PieViewModel 中:public PlotModel salesmodel {get;set;} 这个放在我的 SalesPage.xaml.cs 中:pvm.salesModel = pvm.Model1;我这样做对吗?
-
嗯....你改变了一些东西:
salesmodel不是PieViewModel属性....现在,根据你的改变它是....如果那是在这种情况下,您不需要salesmodel和Model1,只需其中一个就足够了。请不要在 cmets 部分发布代码。相反,请编辑原始问题中的代码。
标签: c# xamarin charts xamarin.forms oxyplot