2016-07-11 11 views
6

Ich habe hinzugefügt OxyPlot Android und Core in meinem Xamarin PCL basierten Projekt, wo ich MVVMCross verwende.Bindung OxyPlot über MVVMCross in Xamarin.Android

Ich habe die Plotview in meinem XML wie folgt hinzugefügt. Aber ich weiß nicht, wie ich diese Ansicht mit MVVMCross binden kann.

Gibt es ein gutes Beispiel oder Ressourcen zu folgen?

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

Zusätzliche Informationen zur OxyPlot-Installation

Ich habe OxyPlot wie folgt über die Paketkonsole hinzugefügt.

Im PCL

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

Im Android

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

Oder Sie können sie auch in Nuget Konsole aus prelease Bibliothek hinzufügen.

+0

Sie sollten wahrscheinlich eine benutzerdefinierte Bindung für dieses und jedes Mal, wenn das Modell geändert wird (RaisePropertyChanged()), machen Sie den gebundenen OxyPlot ungültig. Ungefähr, indem man das grob betrachtet. Ich denke http://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom-bind-properties Ihnen helfen könnten, da man nur einen One-Way-Bindung benötigen sollte. Wenn Sie mehr Hilfe benötigen, werde ich versuchen, ein Beispiel zu geben. – Cyriac

+0

Ich habe gelöscht und den Code neu aufgebaut, die ich geschrieben, es ist wie ein charm.But gearbeitet, wenn Sie mir mit der folgenden verwandten Frage helfen könnten http://stackoverflow.com/questions/38332858/oxyplot-in-recyclerview-mvvmcross -xamarin-android, wäre es so hilfreich – hotspring

Antwort

7

Sie sollten in der Lage sein zu erreichen, was Sie wollen mit Standard-Mvx-Eigenschaft Bindung. Keine benutzerdefinierte Bindung erforderlich.

Beispiel auf Frage zugrunde:

Ansatz 1: Fließend Bindung

Ansichtsmodell

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

Ansicht/Layout

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

Fragment/Code Hinter

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

Ansatz 2: Xml Bindung

Ansichtsmodell

Wie oben

Ansicht/Layout

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

Fragment/Code hinter

keine verbindlichen Code benötigt, so stellen Sie sicher, dass das Layout durch die Bindung inflater auszuführen.

+1

@PlaceHolder sein, ' hotspring

+0

@hotspring, Sie zu 100% richtig sind, habe ich die fehlenden graphControl 'FindById' – Plac3Hold3r

+0

nur wurde die Custom für die Ungültigerklärung des Chart denken, da ich sicher war nicht wie Oxyplot es behandelt. Aber nett! – Cyriac