You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8.0 MAUI中使用OxyPlot无UI显示问题求助

.NET 8 MAUI中OxyPlot无内容显示的解决办法

我在.NET 8.0 MAUI应用中使用OxyPlot时,UI上完全没有内容显示,连坐标轴都看不到。网上找到的示例都是Xamarin项目,求帮忙解决。

我的代码实现:

MainPage.xaml 资源部分:

<ContentPage.Resources>
    <local:OrientationPlotModel x:Key="orientationPlotModel"/>
</ContentPage.Resources>

Frame内的PlotView代码:

<oxy:PlotView x:Name="OrientationDiagramPlotModel" BackgroundColor="White" />

MainPage.xaml.cs:

private OrientationPlotModel orientationPlotModel;

public MainPage()
{
  InitializeComponent();

  orientationPlotModel=new OrientationPlotModel();
  OrientationDiagramPlotModel.BindingContext = orientationPlotModel;
}

OrientationPlotModel.cs:

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace MauiApp2.View
{
  public class OrientationPlotModel
  {
    public PlotModel PlotModel { get; private set; }

    private LineSeries orientationSeries;

    private double[] orientationData = new double[10];

    public OrientationPlotModel()
    {
      InitializePlotModel();
    }

    private void InitializePlotModel()
    {
      PlotModel = new PlotModel();

      var bottomAxis = new LinearAxis
      {
        Position = AxisPosition.Bottom,
        Minimum = 10,
        Maximum = 100,
        Title = "X Axis Title",
        MajorGridlineStyle = LineStyle.Solid, 
        MinorGridlineStyle = LineStyle.Dot,
        AxislineColor = OxyColors.Blue,
      };

      var leftAxis = new LinearAxis
      {
        Position = AxisPosition.Left,
        Minimum = 0,
        Maximum = 180,
        Title = "Y Axis Title",
        MajorGridlineStyle = LineStyle.Solid,
        MinorGridlineStyle = LineStyle.Dot,
      };
      
      PlotModel.Axes.Add(bottomAxis);
      PlotModel.Axes.Add(leftAxis);

      orientationSeries = new LineSeries();

      PlotModel.Series.Add(orientationSeries);

      for (int i = 0; i < orientationData.Length; i++)
      {
        orientationSeries.Points.Add(new DataPoint(i, orientationData[i]));
      }
    }

    public void UpdateOrientation(float orientation)
    {
     //...
    }
  }
}

问题分析与解决步骤:

  1. PlotView未绑定PlotModel属性
    当前代码仅设置了BindingContext,但PlotView需要明确绑定Model属性才能加载图表数据。修改XAML中的PlotView:

    <oxy:PlotView x:Name="OrientationDiagramPlotModel" 
                  BackgroundColor="White" 
                  Model="{Binding PlotModel}" />
    

    或者在后台代码直接赋值:

    OrientationDiagramPlotModel.Model = orientationPlotModel.PlotModel;
    
  2. X轴范围与数据不匹配
    你设置的X轴Minimum=10,但数据点的X值是0-9,完全在轴的显示范围之外,导致图表内容不可见。调整X轴的Minimum为0:

    var bottomAxis = new LinearAxis
    {
      Position = AxisPosition.Bottom,
      Minimum = 0, // 修改此处
      Maximum = 100,
      Title = "X Axis Title",
      MajorGridlineStyle = LineStyle.Solid, 
      MinorGridlineStyle = LineStyle.Dot,
      AxislineColor = OxyColors.Blue,
    };
    
  3. 确认OxyPlot NuGet包版本
    确保安装的是适配.NET MAUI的OxyPlot.Maui包(而非Xamarin版本的OxyPlot.Xamarin.Forms),选择最新稳定版即可。

  4. 检查命名空间引用
    确认XAML头部已正确引入OxyPlot的命名空间:

    xmlns:oxy="clr-namespace:OxyPlot.Maui;assembly=OxyPlot.Maui"
    

完成以上修改后重新运行应用,即可看到坐标轴和图表内容。


内容的提问来源于stack exchange,提问作者Áron Kabucz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 06:02:14