gpt4 book ai didi

android - 在 Xamarin.Android 中通过 MVVMCross 绑定(bind) OxyPlot

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:24:51 25 4
gpt4 key购买 nike

我在我使用 MVVMCross 的基于 Xamarin PCL 的项目中添加了 OxyPlot Android 和 Core。

我已经在我的 xml 中添加了 plotview,如下所示。但我不知道如何使用 MVVMCross 绑定(bind)此 View 。

有什么好的例子或资源可以借鉴吗?

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 安装的附加信息

我已经通过 Package Console 按如下方式添加了 OxyPlot。

在 PCL 中

PM> Install-Package OxyPlot.Core -Version 1.0.0-unstable1983 -Pre

在安卓系统中

PM> Install-Package OxyPlot.Xamarin.Android -Pre

或者您也可以从预发布库中将它们添加到 Nuget 控制台中。

最佳答案

您应该能够使用标准 Mvx 属性绑定(bind)实现您想要的。无需自定义绑定(bind)。

基于问题的示例:

方法 1:流畅绑定(bind)

View 模型

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 /布局

<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent" />

fragment/代码隐藏

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 绑定(bind)

View 模型

同上

View /布局

<oxyplot.xamarin.android.PlotView
android:id="@+id/plot"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model MyModel"/>

fragment/代码隐藏

不需要绑定(bind)代码,只需确保通过绑定(bind) 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);
}

关于android - 在 Xamarin.Android 中通过 MVVMCross 绑定(bind) OxyPlot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38316666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com