通过Xamarin.Android中的MVVMCross绑定OxyPlot

卡西利亚斯

我已经OxyPlot在使用XamarinPCL的项目中添加了Android和Core MVVMCross

我已经在我的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安装的附加信息

我已经通过Package Console添加了OxyPlot,如下所示。

在PCL中

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

在Android中

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

或者,您也可以从prelease库在Nuget控制台中添加它们。

Plac3Hold3r

您应该能够使用标准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); }
    }
}

查看/布局

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

片段/代码后面

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;
}

方法2:Xml绑定

视图模型

同上

查看/布局

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

片段/代码后面

无需绑定代码,只需确保通过绑定充气机运行布局即可。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Xamarin Studio中通过推送通知启动的调试应用

MVVMCross:如何将Xamarin.Android事件绑定到ViewModel命令

我们如何通过mvvmcross绑定在Android的SearchView中输入的搜索文本

无法通过调试Xamarin Android和iOS在设备上部署

如何通过C#/ Xamarin中的Webview链接调用whatsapp?

Xamarin Android通过GetExternalFilesDir()获得privateExternalStorage

如何使用MVVMCross在Xamarin.Android中验证数字

循环Xamarin.iOS中的MvvmCross绑定

通过xamarin.ios中的内容调整UILabel的高度

在Xamarin Android中从MVVMCross中的视图模型关闭键盘

如何通过Xamarin Forms代码从Android Fragment打开或访问数据?

Xamarin Android TabLayout徽章:绑定文本Mvvmcross

将多个值绑定到一个控件Xamarin.Android MVVMCross MVX

Xamarin Android绑定

Xamarin Android MvvmCross入门

如何在Xamarin.Android中通过单个活动通过单击按钮在视图之间导航?

通过Android在Xamarin中使用RestRequest

通过Xamarin表单中的URL获取HTML

使用Mvvmcross将分组数据绑定到Xamarin中的UITableview

无法通过Visual Studio Team Services签名Xamarin Android APK

RecyclerView中的OxyPlot -MVVMCross Xamarin.Android

通过 Xamarin for Android 中的 Web 链接处理深层链接

通过 Xamarin (UWP) 中的 TraceSource 进行日志记录

等待被代码忽略并直接通过 Xamarin.Android

无法通过 Android Xamarin Forms UITest 单击 DisplayAlert 按钮

如何通过 Xamarin 调用 PayTM Android SDK?

通过 Xamarin 连接到 iOS 中已经记住的 SSID

使用 MvvmCross 和 FFImageLoading 的 Xamarin Android 可见性绑定问题

Xamarin Android 通过样式控制文本颜色