通过Xamarin.Android中的MVVMCross绑定OxyPlot

cas*_*las 5 android xamarin.android mvvmcross xamarin oxyplot

我在我正在使用的基于PCL的项目中添加了OxyPlotAndroid和Core .XamarinMVVMCross

我在我的xml中添加了plotview,如下所示.但我不知道如何使用MVVMCross绑定此视图.

是否有任何好的例子或资源可供遵循?

MyView.xml

<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" /> 
Run Code Online (Sandbox Code Playgroud)

MyView.cs

public class MyView : MvxFragment<MyViewModel>
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.MyView, null)

        MyViewModel MyMainViewModel = new MyViewModel();
        var a = view.FindViewById<PlotView>(Resource.Id.plot);
        a.Model = MyViewModel.MyModel;

        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

MyViewModel.cs

public PlotModel MyModel { get; set; }
public MyViewModel
{
  PlotModel mo = new PlotModel();
  var s1 = new LineSeries()
  {
    Color = OxyColors.SkyBlue,
    MarkerType = MarkerType.Circle,
    MarkerSize = 6,
    MarkerStroke = OxyColors.White,
    MarkerFill = OxyColors.SkyBlue,
    MarkerStrokeThickness = 1.5
  };
  s1.Points.Add(new DataPoint(0, 10));
  s1.Points.Add(new DataPoint(10, 40));
  s1.Points.Add(new DataPoint(40, 20));
  s1.Points.Add(new DataPoint(60, 30));
  mo.Series.Add(s1);
  MyModel = mo;
}
Run Code Online (Sandbox Code Playgroud)

有关OxyPlot安装的其他信息

我通过Package Console添加了如下的OxyPlot.

在PCL中

PM> Install-Package OxyPlot.Core -Version 1.0.0-unstable1983 -Pre
Run Code Online (Sandbox Code Playgroud)

在Android中

PM> Install-Package OxyPlot.Xamarin.Android -Pre
Run Code Online (Sandbox Code Playgroud)

或者您也可以从prelease库中将它们添加到Nuget Console中.

Pla*_*d3r 7

您应该能够使用标准Mvx属性绑定实现您想要的功能.无需自定义绑定.

基于问题的示例:

方法1:流利的绑定

视图模型

public class MyViewModel : MvxViewModel
{
    public MyViewModel()
    {
        GeneratePlotPoints();
    }

    void GeneratePlotPoints()
    {
        var mo = new PlotModel();
        var s1 = new LineSeries()
        {
            Color = OxyColors.SkyBlue,
            MarkerType = MarkerType.Circle,
            MarkerSize = 6,
            MarkerStroke = OxyColors.White,
            MarkerFill = OxyColors.SkyBlue,
            MarkerStrokeThickness = 1.5
        };
        s1.Points.Add(new DataPoint(0, 10));
        s1.Points.Add(new DataPoint(10, 40));
        s1.Points.Add(new DataPoint(40, 20));
        s1.Points.Add(new DataPoint(60, 30));
        mo.Series.Add(s1);
        MyModel = mo;
    }

    PlotModel _myModel;
    public PlotModel MyModel
    {
        get { return _myModel; }
        set { SetProperty(ref _myModel, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

查看/布局

<oxyplot.xamarin.android.PlotView
   android:id="@+id/plot"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

片段/代码背后

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignored = base.OnCreateView(inflater, container, savedInstanceState);
    var view = this.BindingInflate(Resource.Layout.MyView, null);

    var graphControl = view.FindViewById<PlotView>(Resource.Id.plot);

    var bindset = this.CreateBindingSet<MyView, MyViewModel>();
    bindset.Bind(graphControl).For(c => c.Model).To(vm => vm.MyModel);
    bindset.Apply();

    return view;
}
Run Code Online (Sandbox Code Playgroud)

方法2:Xml绑定

视图模型

与上述相同

查看/布局

<oxyplot.xamarin.android.PlotView
   android:id="@+id/plot"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   local:MvxBind="Model MyModel"/>
Run Code Online (Sandbox Code Playgroud)

片段/代码背后

不需要绑定代码,只需确保通过绑定inflater运行布局.

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignored = base.OnCreateView(inflater, container, savedInstanceState);
    return this.BindingInflate(Resource.Layout.MyView, null);
}
Run Code Online (Sandbox Code Playgroud)