oxyplot: This PlotModel is already in use by some other PlotView control.
This issue occures at line 165 in PlotViewEx.cs. As my thought, reattaching same plotmodel to the same plotview should not throw this InvalidOperationException showing on title. Then a possible solution is to avoid throwing InvalidOperationException and reattaching plotmodel when this.currentModel.PlotView equals current plotview object(this).
Code modification is here:
private void OnModelChanged()
{
lock (this.modelLock)
{
if (this.currentModel != null)
{
((IPlotModel)this.currentModel).AttachPlotView(null);
}
this.currentModel = this.Model;
if (this.currentModel != null && !Equals(this.currentModel.PlotView, this))
{
if (this.currentModel.PlotView != null)
{
throw new InvalidOperationException(
"This PlotModel is already in use by some other PlotView control.");
}
((IPlotModel)this.ActualModel).AttachPlotView(this);
}
}
this.InvalidatePlot();
}
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 45 (13 by maintainers)
Commits related to this issue
- Add Xamarin Forms ItemTemplate demos (for #497) — committed to objorke/oxyplot by objorke 9 years ago
- Add Xamarin Forms ItemTemplate demos (for #497) — committed to objorke/oxyplot by objorke 9 years ago
- Add Xamarin Forms ItemTemplate demos (for #497) — committed to objorke/oxyplot by objorke 9 years ago
- Add Xamarin Forms ItemTemplate demos (for #497) — committed to objorke/oxyplot by objorke 9 years ago
- #156 Fixed issue with "PlotModel is already in use" that came back from the grave after creating LogQueryResults to group queries. Workaround uses SafePlotModel: https://github.com/oxyplot/oxyplot/iss... — committed to simionsoft/SimionZoo by borjafdezgauna2 6 years ago
I know this is old and done, but until proper refactoring mentioned above is complete. Using PlotModel in a ViewModels and dynamically generating Views (ex. TabControls with DataTemplates) is not possible with out having the exception thrown once in a while.
So I though I would share a “simple” hack work around.
Instantiate this type instead of PlotModel in your VM and the most recent DataTemplate View will be auto attached.
I really like the solution offered by @edvinasz but I had a case where I couldn’t use a derived class from PlotModel so I created this extension class:
Use it simply like this:
@angularsen thank you very much, the problem is already solved: https://github.com/oxyplot/oxyplot/issues/497#issuecomment-253437909
Yes @IronLlama.
The InvalidateFlag is changed in VM whenever it needs to redraw the entire thing, such as when new points are added to the series. Similar to calling
Invalidatemethods on the plot model itself.