【问题标题】:Add OxyPlot view in a grid在网格中添加 OxyPlot 视图
【发布时间】:2015-07-21 13:43:37
【问题描述】:

我有这个类,继承了ContentPage

public class MainPage : ContentPage
{

    public Label lblTime { get; set; }

    public Grid grid { get; set; }

    public MainPage ()
    {

        //some code which doesn't matter

        lblTime = new Label {
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Text = currentTime
        };

        Grid grid = new Grid
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            RowDefinitions = 
            {
                new RowDefinition { Height = new GridLength(40, GridUnitType.Auto) },
                new RowDefinition { Height = new GridLength() }
            },
            ColumnDefinitions = 
            {
                new ColumnDefinition { Width = new GridLength(50, GridUnitType.Auto) },
                new ColumnDefinition { Width = new GridLength(50, GridUnitType.Star) }
            }
        };

        grid.Children.Add (lblTime, 0, 1, 0, 1);

        Content = new StackLayout { 
            Children = {
                grid
            }
        };
    }
}

我也有这个类,绘制图形:

public class DrawGraph
{
    public PlotModel MyModel { get; set; }

    public DrawGraph ()
    {

        var plotModel = new PlotModel {};

        plotModel.Title = "Test";

        var xaxis = new LinearAxis {
            Position = AxisPosition.Bottom
        };

        var yaxis = new LinearAxis {
            Position = AxisPosition.Left
        };

        yaxis.TickStyle = OxyPlot.Axes.TickStyle.Inside;
        xaxis.MinorTickSize = 20;
        yaxis.TicklineColor = OxyColors.Blue;

        plotModel.Axes.Add (xaxis);
        plotModel.Axes.Add (yaxis);

        var series1 = new LineSeries {
            StrokeThickness = 3,
            MarkerType = MarkerType.Circle,
            MarkerSize = 4,
            MarkerStroke = OxyColors.LimeGreen,
            MarkerStrokeThickness = 1
        };

        double[,] chartValues = new double[,] {{1437041279684.0156,1.0897299999999999},{1437041339421.4324,1.0896},{1437041397111.7322,1.0897100000000002},{1437041458595.2488,1.0898599999999998},{1437041519510.7329,1.08992},{1437041578914.1306,1.08983},{1437041639357.5877,1.0895100000000002},{1437041699721.0405,1.0895299999999999},{1437041758381.3958,1.0895000000000001},{1437041818605.84,1.08954},{1437041879932.3479,1.0894400000000002},{1437041939531.7568,1.08902}};

        for (int i = 0; i < chartValues.GetLength(0)/2; i++) {
            series1.Points.Add (new DataPoint (chartValues[i,0],chartValues[i,1]));
        }

        plotModel.Series.Add (series1);

        this.MyModel = plotModel;
    }
}

我的问题是,如何调用 DrawGraph 类并将其生成的视图放入网格单元格中?我想这将类似于从 DrawGraph 类调用生成的 plotView,如下所示: grid.Children.Add (plotView, 0, 1, 1, 2);但我想不通。

【问题讨论】:

    标签: android xamarin xamarin.android xamarin.forms oxyplot


    【解决方案1】:

    您好,最好的方法是使用绑定将您的视图与视图模型挂钩。 PlotView 到 DrawGraph

    在这种情况下,您必须进行以下更改:

    在您的 ContentPage 中,我们必须创建一个 PlotView 并将其添加到网格中,这就是图表的渲染器,

    var graph = new PlotView ();
    grid.Children.Add (graph, 0, 0, 1, 0);
    

    接下来我们要告诉图我们要绑定什么,在这种情况下,我们将 ModelProperty 绑定到 ViewModel(我们的 DrawGraph 类)上的 MyModel,我们还需要将页面的 BindingContext 设置为新的 DrawGraph 实例。

    graph.SetBinding(PlotView.ModelProperty, new Binding ("MyModel"));
    this.BindingContext = new DrawGraph ();
    

    我们的 DrawGraph 类需要进行一些更改,以便他能够正确更新 UI,我们需要实现 INPC 接口。所以我们添加这个:

    public class DrawGraph : INotifyPropertyChanged
        {
           public event PropertyChangedEventHandler PropertyChanged;
    
           public void OnPropertyChaged([CallerMemberName]string caller = null){
                var handler = PropertyChanged;
                if (handler != null) {
                    handler(this, new PropertyChangedEventArgs(caller));
                }
           }
    

    我们需要更改属性设置器以在更改时通知:

    PlotModel myModel;
    public PlotModel MyModel {
        get{ return myModel; }
        set {
            myModel = value;
            OnPropertyChaged ();
        }
    }
    

    这应该是您获得图表渲染所需要的。

    每次设置新的 MyModel 属性时,它都会更新 UI。

    【讨论】:

    • 我已完成所有步骤,但在创建 var "graph" 时出错:Error CS0103: The name 'PlotView' does not exist in the current context.
    • 确保带上所有正确的命名空间并将 Oxyplot Xamarin Forms 版本添加到所有项目中
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多