locked
How to add OxyPlot to a Xamarin Forms xaml Page RRS feed

  • Question

  • User121031 posted

    Hi.

    Im trying to include oxyplot into a Xamarin.Forms-Project

    This is my Xaml.cs file where Im building the PlotModel.

    ` private void GeneratePlot() { var Points = new List { new DataPoint(0, 0), new DataPoint(10, 5), new DataPoint(20, 10), new DataPoint(30, 16), new DataPoint(40, 12), new DataPoint(50, 19) }; var Points2 = new List { new DataPoint(0, 0), new DataPoint(10, 7), new DataPoint(20, 8), new DataPoint(30, 8), new DataPoint(40, 20), new DataPoint(50, 25) }; var m = new PlotModel(); m.PlotType = PlotType.XY; m.InvalidatePlot(true); m.Title = "hello oxy"; m.ResetAllAxes(); var ls1 = new LineSeries(); var ls2 = new LineSeries(); ls1.ItemsSource = Points; ls2.ItemsSource = Points2;

        m.Series.Add(ls1);
        m.Series.Add(ls2);
        var _opv = new OxyPlotView
        {
            WidthRequest = 300,
            HeightRequest = 300,
            BackgroundColor = Color.Aqua
        };
        _opv.Model = m;
        PlModel = m;
    }
    

    `

    In Code this works fine when I just put the OxyPlotModel _opy into a StackLayout. But when Im going to add it into Xaml like this:

    <oxy:PlotView Model="{Binding Model}" VerticalOptions="Center" HorizontalOptions="Center" />

    Im getting this Exception:

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: An exception was thrown by the type initializer for OxyPlot.XamarinForms.PlotView ---> System.MissingMethodException: Method not found: 'Xamarin.Forms.BindableProperty.Create'

    Does anyone know how to include oxyPlot into a XamlPage?

    In my References Xamarin.Forms.Core and Xamarin.Forms.Xaml is Version 1.4 Xamarin.Forms.Platform is 1.0. OxyPlot and OxyPlot.XamarinForms is 2014.1.517.0

    Thanks in advance.

    Wednesday, April 22, 2015 11:58 AM

All replies

  • User3466 posted

    @Micha.7611 can you show us your entire XAML?

    Thursday, April 23, 2015 12:52 PM
  • User98295 posted

    @Micha.7611 since you're building your PlotModel in your code behind file you may need to change the way you bind it to your PlotView. You could give your PlotView an x:Name attribute then use SetBinding in your code behind to bind it to the PlotModel. If you go that route you'll want to remove the "Model={Binding Model}" from your Xaml file.

    Another, probably better, option would be to create your PlotModel in a view model, then set your Page's BindingContext to that view model. Then your Xaml with the Binding will work as it is.

    Thursday, April 23, 2015 2:03 PM
  • User121031 posted

    Hi,

    I tried it with a ViewModel now but I still get the same Error:

    This is my Xaml (there are a few other Stacklayouts inside the StackLayout in the ContentPage):

    <?xml version="1.0" encoding="utf-8"?>
    
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="App.Pages.Page1"
                 xmlns:oxy="clr-namespace:OxyPlot.XamarinForms;assembly=OxyPlot.XamarinForms">
    
     <StackLayout>
    <oxy:PlotView  Model="{Binding PlotModel}" VerticalOptions="Center" HorizontalOptions="Center" />
     </StackLayout>
    </ContentPage>
    

    This is my ViewModel:

    public class PageViewModel : INotifyPropertyChanged
        {
    private OxyPlotView _oxyPlotViewModel = new OxyPlotView();
    
            public OxyPlotView OxyPlotViewModel
            {
                get { return _oxyPlotViewModel; }
                set
                {
                    if (_oxyPlotViewModel != value)
                    {
                        _oxyPlotViewModel = value;
                        OnPropertyChanged("PlotModel");
                    }
                }
            }
    
            public void SetPlotModel(OxyPlotView oxyPlotViewModel)
            {
                OxyPlotViewModel = oxyPlotViewModel;
            }
    
     protected virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs(propertyName));
                }
            }
    }
    

    And this is my Xaml.cs:

    public partial class Page1
    {
    private OxyPlotView _opv = new OxyPlotView();
    
           public Page1()
            {
                InitializeComponent();
                GeneratePlot();
                BindingContext = viewModel;
            }
    
            private void GeneratePlot()
            {
                var Points = new List<DataPoint>
                {
                    new DataPoint(0, 0),
                    new DataPoint(10, 5),
                    new DataPoint(20, 10),
                    new DataPoint(30, 16),
                    new DataPoint(40, 12),
                    new DataPoint(50, 19)
                };
                var Points2 = new List<DataPoint>
                {
                    new DataPoint(0, 0),
                    new DataPoint(10, 7),
                    new DataPoint(20, 8),
                    new DataPoint(30, 8),
                    new DataPoint(40, 20),
                    new DataPoint(50, 25)
                };
                var m = new PlotModel();
                m.PlotType = PlotType.XY;
                m.InvalidatePlot(true);
                m.Title = "hello oxy";
                m.ResetAllAxes();
                var ls1 = new LineSeries();
                var ls2 = new LineSeries();
                ls1.ItemsSource = Points;
                ls2.ItemsSource = Points2;
    
                m.Series.Add(ls1);
                m.Series.Add(ls2);
                _opv = new OxyPlotView
                {
                    WidthRequest = 300,
                    HeightRequest = 300,
                    BackgroundColor = Color.Aqua
                };
                _opv.Model = m;
                viewModel.SetPlotModel(_opv);
    
            }
    }
    

    But Im still gettin this Exception:

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: An exception was thrown by the type initializer for OxyPlot.XamarinForms.PlotView ---> System.MissingMethodException: Method not found: 'Xamarin.Forms.BindableProperty.Create'.

    Does anyone get this to work with Xaml and ViewModel?

    Thanks in advance.

    Monday, April 27, 2015 12:37 PM
  • User121031 posted

    Ok, reselect and click on the C works ;)

    The upper comment is complete now

    Monday, April 27, 2015 12:41 PM
  • User3466 posted

    @Micha.7611 paste your code into the edit box, then reselect all of it. Then click the C on the toolbar just above the edit box.

    Monday, April 27, 2015 12:56 PM
  • User121031 posted

    Ok, thx.

    The upper comment is complete now.

    Monday, April 27, 2015 1:01 PM
  • User121031 posted

    Btw this is what im using in my References: Xamarin.Forms.Core and Xamarin.Forms.Xaml is 1.4 Xamarin.Forms.Platform is 1.0. OxyPlot and OxyPlot.XamarinForms is 2014.1.517.0

    In the Android Section: OxyPlot, OxyPlot.Xamarin.Android and OxyPlot.XamarinForms.Android is also 2014.1.517.0

    Does someone get this to work in xaml? If yes with wich versions?

    Thanks

    Monday, April 27, 2015 1:56 PM
  • User101701 posted

    Could it be, that you are wrong with the namespace?

    Try this: xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"

    instead of: xmlns:oxy="clr-namespace:OxyPlot.XamarinForms;assembly=OxyPlot.XamarinForms"

    Monday, May 11, 2015 12:37 PM
  • User98295 posted

    @Micha.7611 it looks like the PlotView in your XAML file has its Model set to "{Binding PlotModel}" but there is not a property on your VM called "PlotModel". I believe that explains the Exception you posted. You'll either need to move your PlotModel to your VM or skip the binding and set the PlotModel in your code behind.

    Monday, May 11, 2015 12:55 PM
  • User141005 posted

    @Micha.7611 Did you ever resolve this? I am running into the same issue?

    As of today, I am pointing to the latest pre-release of everything. Are there any correlated assemblies that I should be aware of that I can pair and things just work?

    Wednesday, September 2, 2015 1:55 AM
  • User208792 posted

    @Micha.7611 what are the Libraries you use in Xaml.cs and ViewModel?

    Tuesday, July 19, 2016 2:27 AM