在 Xamarin.Android 中通过 MVVMCross 绑定 OxyPlot
Binding OxyPlot via MVVMCross in Xamarin.Android
我在我使用 MVVMCross
.
的基于 Xamarin
PCL 的项目中添加了 OxyPlot
Android 和核心
我在 xml 中添加了 plotview,如下所示。但我不知道如何使用 MVVMCross 绑定此视图。
有什么好的例子或资源可以参考吗?
MyView.xml
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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;
}
}
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;
}
OxyPlot 安装的附加信息
我已经通过程序包控制台按如下方式添加了 OxyPlot。
在PCL
PM> Install-Package OxyPlot.Core -Version 1.0.0-unstable1983 -Pre
在Android
PM> Install-Package OxyPlot.Xamarin.Android -Pre
或者您也可以从预发布库中将它们添加到 Nuget 控制台中。
您应该能够使用标准 Mvx 属性 绑定实现您想要的。无需自定义绑定。
基于问题的示例:
方法 1:流畅绑定
ViewModel
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); }
}
}
View/Layout
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Fragment/Code落后
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;
}
方法二:Xml绑定
ViewModel
同上
View/Layout
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model MyModel"/>
片段/背后代码
无需绑定代码,只需确保 运行 通过绑定 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);
}
我在我使用 MVVMCross
.
Xamarin
PCL 的项目中添加了 OxyPlot
Android 和核心
我在 xml 中添加了 plotview,如下所示。但我不知道如何使用 MVVMCross 绑定此视图。
有什么好的例子或资源可以参考吗?
MyView.xml
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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;
}
}
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;
}
OxyPlot 安装的附加信息
我已经通过程序包控制台按如下方式添加了 OxyPlot。
在PCL
PM> Install-Package OxyPlot.Core -Version 1.0.0-unstable1983 -Pre
在Android
PM> Install-Package OxyPlot.Xamarin.Android -Pre
或者您也可以从预发布库中将它们添加到 Nuget 控制台中。
您应该能够使用标准 Mvx 属性 绑定实现您想要的。无需自定义绑定。
基于问题的示例:
方法 1:流畅绑定
ViewModel
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); }
}
}
View/Layout
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Fragment/Code落后
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;
}
方法二:Xml绑定
ViewModel
同上
View/Layout
<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model MyModel"/>
片段/背后代码
无需绑定代码,只需确保 运行 通过绑定 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);
}