【发布时间】:2020-03-31 19:57:55
【问题描述】:
我正在尝试解决这个问题大约 3 天。
TL;DR: 如何将几个 OxyPlots(不同种类)放在可滚动的 ScrollPage 中(在 ContentPage 上)? 有人能解释一下 XAML 和 CS 代码库中的哪个元素是什么关系吗?
这个问题看起来很简单,但我很难做到这一点。每个页面都显示相同的示例,或者对我的问题不够具体,只有零散的问题。 基本上,我想这样做:
所以现在我遇到了一些问题,比如图形填满整个站点、图形不显示、由于某种原因我无法使用 MultiView 等等。
大部分教程都依赖于将页面的整个内容设置为图表。其他一些人可以绕过它,但只能使用无法工作的神秘 XAML 或 XAML(MultiView,我使用 ScrollPage 而不是 ContentPage,因此我无法设置 <ContentPage.BindingContext/> 属性或 overrides void OnAppearing()。
在 CS 中通过代码设置 BindingContext 也无济于事,因为这似乎只适用于单图表页面。
(我初始化了渲染器。我的应用中已经显示了图表。)
这就是我的理解:
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
mc:Ignorable="d"
x:Class="PieCharter.Test">
<ScrollView.Content>
<oxy:PlotView Model="{Binding Model}" IsVisible="True"/>
</ScrollView.Content>
</ScrollView>
我可以在 XAML 中设置一个(或多个)这些图表,它们应该显示为具有此代码库的页面内容:
using System;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Xamarin;
using OxyPlot.Xamarin.Forms;
namespace PieCharter
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Test : ScrollView
{
public Test()
{
InitializeComponent();
Content = new PlotView
{
Model = CreatePieChart()
};
}
private PlotModel CreatePieChart()
{
var model = new PlotModel { Title = "PieChart" };
var ps = new PieSeries
{
StrokeThickness = 0.25,
InsideLabelPosition = 0.25,
AngleSpan = 360,
StartAngle = 0
};
ps.Slices.Add(new PieSlice("Slice 1, 50");
ps.Slices.Add(new PieSlice("Slice 2, 50");
ps.Slices.Add(new PieSlice("Slice 3, 50");
model.Series.Add(ps);
return model;
}
}
}
但这给我带来了问题,因为它将整个页面设置为图表,我无法将它们划分为例如一个网格视图(在使用 Layout 容器和多个 PlotViews 时也没有帮助)。
为什么我不能只创建几个private PlotView PiePlotView,给它们Model = CreateXChart() 作为参数,并将我的页面在XAML 文件中的内容设置为PlotViews?
这真的让我很困惑,不幸的是,文档并没有真正的帮助。
Model="{Binding Model}" 究竟是什么意思? Model 是 PlotView 的成员吗? (应该是,但我见过人们使用其他变量。)如果是这样,当将多个 OxyPlot 放入一个文件时,我的 XAML 怎么知道使用哪个 Model?我想这与BindingContext/<ContentPage.BindingContext/> 或其他东西有关。
谁能更详细地向我解释一下如何实现这一点,如果你不提供解决方案,请帮助我至少了解发生了什么。这很烦人,老实说,我不知何故认为 XAML 似乎是一种非常不优雅和混乱的解决方案(至少在我看来是这样的)。
我了解 XAML 的基础知识,但我一无所知。但这只是这两个部分之间的交互,我无法理解......
我已经尝试过的(还有更多,但可能有各种错误):
【问题讨论】:
-
使用 Layout 容器 - StackLayout、Grid 等 - 在一个页面(或 ScrollView)中包含多个视图
-
我应该提到我已经使用了它。我会马上做的
-
Model="{Binding Model}" 究竟是什么意思? => 你了解 Binding 和 BindingContext 吗? View 的绑定上下文默认设置为其父级的 BindingContext。这里ScrollView的BindingContext就是你要添加ScrollView的View的BindingContext,我猜是Page????
-
这就是问题所在,我知道我可以使用容器在一个页面上托管多个项目。但有了情节,这似乎并不容易。它只是行不通。我的问题是,正如您正确理解的那样:如何在单个页面上发布多个图。
-
模型通常是驱动图表的数据。例如,您可以有一个折线图和一个条形图,它们都以不同的方式显示相同的数据/模型。
标签: c# xaml xamarin xamarin.forms oxyplot