gpt4 book ai didi

c# - Xamarin Forms 可移植应用程序上未显示 OxyPlot 图表

转载 作者:行者123 更新时间:2023-11-30 16:01:36 27 4
gpt4 key购买 nike

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

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

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

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

最佳答案

下面是一些示例代码:

应用:(可移植)

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

View 模型:

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:(机器人)

 [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

关于c# - Xamarin Forms 可移植应用程序上未显示 OxyPlot 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com