简体   繁体   English

Xamarin.Forms:单个 ScrollView 上的几个 Oxyplots

[英]Xamarin.Forms: Several Oxyplots on a single ScrollView

I am trying to figure this out for about 3 days now.我现在正在尝试解决这个问题大约 3 天。

TL;DR: How can I put several OxyPlots (of different kinds) in a scrollable ScrollPage (on a ContentPage)? TL;DR:如何将几个 OxyPlots(不同种类的)放入可滚动的 ScrollPage(在 ContentPage 上)? Can someone explain me in which relationship which element in XAML and the CS Codebase is?有人可以解释一下 XAML 和 CS 代码库中的哪个元素的关系吗?


The question seems simple, but I struggle to get this going.这个问题看起来很简单,但我很难做到这一点。 Every page shows the same examples or is not specific enough for my problem, only scattered problems.每个页面都显示相同的示例,或者对于我的问题不够具体,只有零散的问题。 Basically, I want to do this:基本上,我想这样做:

在此处输入图片说明

So now I encounter a few problems like the graph fills the whole site, the graph doesnt display, I cant use MultiView for some reason, etc. etc.所以现在我遇到了一些问题,比如图表填满了整个网站,图表不显示,由于某种原因我不能使用 MultiView 等等。

Most of the tutorials rely on setting the whole content of the page as the chart.大多数教程依赖于将页面的整个内容设置为图表。 Some others can bypass that but only with cryptic XAML or XAML that wont work (MultiView, I use a ScrollPage instead of a ContentPage, so I cant set the <ContentPage.BindingContext/> attribute, or overrides void OnAppearing() . Also setting the BindingContext via Code in CS wont help, because it seems that that only applies to single- chart- pages.其他一些人可以绕过它,但只能使用无法正常工作的神秘 XAML 或 XAML(MultiView,我使用 ScrollPage 而不是 ContentPage,因此我无法设置<ContentPage.BindingContext/>属性,或overrides void OnAppearing() 。同时设置通过 CS 中的代码BindingContext无济于事,因为这似乎只适用于单图表页面。

(I initialized the renderer. I already had charts being shown in my app.) (我初始化了渲染器。我的应用程序中已经显示了图表。)

So this is how I understand it:所以我是这样理解的:

<?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>

I can set one (or more) of those charts here in XAML, and they should appear as the page content with this codebase:我可以在 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;
        }
    }
}

But this gives me problems because it sets the whole page to the chart and I cant compartmentalize them into eg a Grid view (also while using Layout container and multiple PlotView s it didnt help).但这给我带来了问题,因为它将整个页面设置为图表,我不能将它们划分为例如 Grid 视图(在使用 Layout 容器和多个PlotView也没有帮助)。

Why cant I just create a few private PlotView PiePlotView , give them the Model = CreateXChart() as argument and set the content of my page in the XAML files as the PlotView s?为什么我不能只创建一些private PlotView PiePlotView ,给它们Model = CreateXChart()作为参数并将我的页面在 XAML 文件中的内容设置为PlotView s?

It really confuses me, and the documentation is not really helpful, unfortunately.这真的让我感到困惑,不幸的是,文档并没有真正的帮助。

What does the Model="{Binding Model}" actually mean? Model="{Binding Model}"实际上是什么意思? Is Model a member of PlotView ? ModelPlotView的成员吗? (It should be, but I've seen people using other variables.) And if so, how would my XAML know which Model to use when putting multiple OxyPlots into one file? (应该是这样,但我见过有人使用其他变量。)如果是这样,当将多个 OxyPlots 放入一个文件时,我的 XAML 如何知道要使用哪个Model I suppose it has to do with the BindingContext / <ContentPage.BindingContext/> or something.我想它与BindingContext / <ContentPage.BindingContext/>或其他东西有关。

Can someone please explain it to me in a bit more detail how to accomplish this, and if you dont provide a solution, please help me at least understanding what is going on at all.有人可以更详细地向我解释如何实现这一点,如果您不提供解决方案,请帮助我至少了解发生了什么。 Its irritating and I honestly somehow think that XAML seems to be quite the inelegant and messy solution (at least it looks to me like that).它令人恼火,老实说,我以某种方式认为 XAML 似乎是相当不优雅和凌乱的解决方案(至少在我看来是这样)。

I have knowledge of the basics of XAML, I dont know nothing.我了解 XAML 的基础知识,但我什么都不知道。 But its just the interaction between these two parts I cant wrap my head around...但这只是这两部分之间的相互作用,我无法理解......


What I already tried (and many more, but probably got various things wrong):我已经尝试过的(还有更多,但可能有各种错误):

1 , 2 , 3 , 4 , 5 , ... 1 , 2 , 3 , 4 , 5 , ...

ScrollView can have only one children, so you can add a children which in turn can have multiple children like StackLayout or Grid ScrollView 只能有一个子项,因此您可以添加一个子项,而子项又可以有多StackLayout ,例如StackLayoutGrid

Model refers to current model for the graph/chart, you have to set it , if you are using Binding the page needs to have same BindingContext mapped to the viewmodel having property with same name, and Viewmodel should implement INotifyPropertyChanged模型是指图形/图表的当前模型,您必须设置它,如果您使用绑定页面需要将相同的 BindingContext 映射到具有相同名称的属性的视图模型,并且视图模型应实现INotifyPropertyChanged

In code behind of content page在内容页面的代码后面

public TestPage()
{
    BindingContext = new TestViewModel();
}

TestViewModel测试视图模型

public class TestViewModel : INotifyPropertyChanged 
{
    public TestViewModel () 
    {
        //initialize Model 1..4
    }

    public Model Model1 
    {
        get => _Model1;

        set 
        {
            _Model1 = value;
            //set value
            NotifyPropertyChanged (); // implemented method for INotifyPropertyChanged
        }
    }

}
<?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: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">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
                <Label Text="Some label1 " />
                <Label Text="And some stuff" />
                <PlotView x:Name="plotview1" 
                    Model="{Binding Model1}" 
                    IsVisible="True" 
                    HeightRequest="300" />
                <PlotView x:Name="plotview2" 
                    Model="{Binding Model2}" 
                    IsVisible="True" 
                    HeightRequest="300" />
                <PlotView x:Name="plotview3" 
                    Model="{Binding Model3}" 
                    IsVisible="True" 
                    HeightRequest="300" />
                <PlotView x:Name="plotview4" 
                    Model="{Binding Model4}" 
                    IsVisible="True" 
                    HeightRequest="300" />
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM