Search code examples
c#xamarinxamarin.androidmvvmcrossoxyplot

Xamarin Android Oxyplot refresh doesnt work


I have a problem with refreshing Oxyplot graph in Xamarin Android. I'm using Model.InvalidatePlot(true) but still doesnt work.

In my ViewModel I have property PlotModel and method for Refresh like code below. GroupSales is my MvxObservableCollection of GroupSale.

    public void RefreshTest()
        {
            GroupSales.Clear();
            var groupSales = DemoData.GetGroupSaleList(_shop.Id);

            groupSales.Add(new GroupSale()
            {
                Name = "Test",
                Sum = 5555,
                Color = Constants.DefaultColor
            });

            foreach (var item in groupSales)
            {
                GroupSales.Add(item);
            }

            Model = OxyPlotModelCreator.GetGroupSalesModel(GroupSales);
            Model.InvalidatePlot(true);
        }

private PlotModel _model;
        public PlotModel Model
        {
            get { return _model; }
            set
            {
                _model = value;
                RaisePropertyChanged(() => Model);
            }
        }

After call method RefreshTest my MvxObservableCollection is updated and Model too but still looking same in view. Only when Im change mobile orientation its update (cuz Im using two views for portrait and landscape so its initialized again) but I need refresh PlotModel after click on Refresh button.

I tried already call this method in Android fragment and Invalidete PlotView and Model like this:

Button button = _view.FindViewById<Button>(Resource.Id.refreshButton);
            button.Click += delegate
            {
                ViewModel.RefreshTest();
                if (plotView != null && ViewModel.Model != null)
                {
                    plotView.InvalidatePlot(true);
                    plotView.Invalidate();
                    plotView.RefreshDrawableState();
                    plotView.Model.InvalidatePlot(true);
                }
            };

But still doesnt work.... Can someone help me?

EDIT

I Initialize Model in Fragment like this:

 var plotView = _view.FindViewById<PlotView>(Resource.Id.groupSalesModel);
            if (plotView != null && ViewModel.Model != null)
            {
                plotView.Model = ViewModel.Model;
            }

Solution

  • Oh I got it... I just bind in fragment like this:

      var bindset = this.CreateBindingSet<GroupSalesFragment, GroupSalesViewModel>();
                bindset.Bind(plotView).For(c => c.Model).To(vm => vm.Model);
                bindset.Apply();
    

    And its working now....