I’m trying to implement custom annotations using the MVVM pattern. I’ve been using your tutorial as an example (https://www.scichart.com/databinding-annotations-with-mvvm/). Everything works as expected if I manually load some example data to the annotations collection. When I actually try to load the data from our database using async method, the annotations are no longer drawn.
Is this by design or am I doing something wrong? Do you have any suggestions how to load annotations when using async code and MVVM patterns? I haven’t had any problems loading the actual time series on the chart using the same pattern.
I also tried binding the SciChartSurface’s Annotations property to AnnotationCollection and then calling ChartAnnotations.ParentSurface.InvalidateElement(), but the issue still persists.
You can easily reproduce this behavior by adding an async call to the examples’s source code:
private async Task Initialize()
{
var ds0 = new XyDataSeries<double, double>();
var someData = new RandomWalkGenerator().GetRandomWalkSeries(200); // RandomWalkGenerator is found in the examples source code
ds0.Append(someData.XData, someData.YData);
_chartSeries = new ObservableCollection<IChartSeriesViewModel>();
_chartSeries.Add(new ChartSeriesViewModel(ds0, new FastLineRenderableSeries()));
// Now create the labels
_chartLabels = new List<LabelViewModel>
{
new LabelViewModel(5, -2.5, "Label0", "Label0 Tooltip!"),
new LabelViewModel(20, -2, "Label1", "Label1 Tooltip!"),
new LabelViewModel(35, 3, "Label2", "Label2 Tooltip!"),
new LabelViewModel(50, 1.5, "Label3", "Label3 Tooltip!"),
};
await Test();
_chartLabels.Add(new LabelViewModel(65, -0.5, "AFTER ASYNC", "Label4 Tooltip!"));
}
private async Task Test()
{
await Task.Delay(5000);
}
- Juho asked 7 years ago
- last edited 7 years ago
- You must login to post comments
At the point where you create Annotations and add them to SciChartSurface.Annotations, you must be on the UI thread.
This is a limitation of WPF and Windows: Annotations are UIElements and as a result, all UIElements must be created and modified on the UI Thread.
Best regards
Andrew
- Andrew Burnett-Thompson answered 7 years ago
-
But isn’t the whole point of the async/await pattern that you resume to UI context after awaiting an async method call? Adding new LineRenderableSeriesViewModels after the await call works just fine, but I don’t understand why the annotations won’t. Is the binding of annotation viewmodels somehow different?
-
I’m adding an answer below as comments won’t let me format code.
- You must login to post comments
Add this code to various places in your your sample to check which thread you are on:
bool isOnUIThread = Dispatcher.FromThread(Thread.CurrentThread) != null;
Console.WriteLine(isOnUIThread ? "On UI Thread!!" : "Not on UI Thread ...");
(Note: Thread detection taken from here. IT may or may not be correct. Check the ThreadIDs to be sure)
When you are on the UI Thread you can create WPF FrameworkElements and access WPF DependencyProperties. When you are not on the UI Thread, you cannot.
My guess is that you have written or taken some code from one of our forum posts to transform LabelViewModel into a TextAnnotation, but because this code is synchronous, at the point where you actually create the annotation, you are not on the UI thread.
Try it – some debugging and let me know.
- Andrew Burnett-Thompson answered 7 years ago
- last edited 7 years ago
- You must login to post comments
Please login first to submit.