This is a migrated thread and some comments may be shown as answers.

Async Data bound to VisualizationLayer => Must create DependencySource on same Thread as the DependencyObject Exception

5 Answers 1060 Views
Map
This is a migrated thread and some comments may be shown as answers.
Markus
Top achievements
Rank 1
Veteran
Markus asked on 14 Jul 2020, 07:10 AM

Hi,

I have the following problem.
On my map I have a visualization layer with an item template.
If I load the data synchronously, there is no problem. But if I changed zu async load, then I get the "Must create DependencySource on same Thread as the DependencyObject" Exception.

            <telerik:RadMap x:Name="OverviewMap"
                            Grid.Column="1"
                            Center="{Binding Center, Mode=TwoWay}"
                            MouseLocationIndicatorVisibility="Hidden"
                            ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}">

                <telerik:VisualizationLayer ItemsSource="{Binding Jobs}"
                                            SelectionChanged="VisualizationLayer_OnSelectionChanged"
                                            UseBitmapCache="False">
                    <telerik:VisualizationLayer.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  telerik:MapLayer.BaseZoomLevel="10"
                                  telerik:MapLayer.Location="{Binding Location}">
                                <Path Data="{Binding PathData, Mode=OneWay}"
                                      Fill="LightSlateGray"
                                      Stretch="Uniform"
                                      Stroke="{Binding StrokeColor, Mode=OneWay}">

                                    <ToolTipService.ToolTip>
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock FontWeight="Bold" Text="{Binding CustomerName, Mode=OneWay}" />
                                            <TextBlock>
                                                <Run Text="{Binding UnitNumber, Mode=OneWay}" />
                                                <Run Text=" - " />
                                                <Run Text="{Binding UnitType, Mode=OneWay}" />
                                            </TextBlock>
                                            <TextBlock Text="{Binding Information, Mode=OneWay}" />
                                            <Border BorderBrush="Black" BorderThickness="1" />
                                            <TextBlock Text="{Binding PositionInformation, Mode=OneWay}" />
                                        </StackPanel>
                                    </ToolTipService.ToolTip>
                                </Path>
                            </Grid>
                        </DataTemplate>
                    </telerik:VisualizationLayer.ItemTemplate>
                </telerik:VisualizationLayer>

                <telerik:VisualizationLayer ItemsSource="{Binding Technicians, Mode=OneWay}"
                                            SelectionChanged="VisualizationLayer_OnSelectionChanged"
                                            UseBitmapCache="False">
                    <telerik:VisualizationLayer.ItemTemplate>
                        <DataTemplate>
                            <Grid HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  telerik:MapLayer.BaseZoomLevel="10"
                                  telerik:MapLayer.Location="{Binding Location, Mode=OneWay}">
                                <StackPanel HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Orientation="Vertical">
                                    <Path HorizontalAlignment="Center"
Data="M19.359996,22.671184C19.660996,22.671184,19.961996,22.771184,20.262996,22.771184L18.857996,24.476198C18.557996,24.878201 18.757996,25.580207 19.259996,25.98121 19.760996,26.382214 20.462996,26.483214 20.864995,26.082211L22.168995,24.476198C22.368996,24.878201 22.469996,25.279205 22.469996,25.680208 22.469996,27.386222 21.064996,28.690232 19.459996,28.690232 19.158996,28.690232 18.857996,28.589231 18.557996,28.489231L15.948997,31.699257C15.647996,32.10026 15.046997,32.10026 14.544997,31.699257 14.042997,31.298254 13.942997,30.696249 14.243997,30.295245L16.851997,27.085219C16.550996,26.583215 16.350996,26.082211 16.350996,25.480206 16.350996,23.975194 17.754997,22.671184 19.359996,22.671184z M6.9209985,18.759152C8.6269982,20.063162 10.632998,20.765168 12.939997,20.765168 15.146997,20.765168 17.252997,20.063162 18.958996,18.759152 22.870995,19.260156 25.879996,22.571183 25.879996,26.683216L25.879996,30.094244 18.456996,30.094244 18.857996,29.693241 19.359996,29.693241C21.566996,29.693241 23.372996,27.887226 23.372996,25.680208 23.372996,25.078203 23.271996,24.577199 23.071995,24.075195L22.268996,22.972186 21.767996,22.671184 20.563996,21.868177C20.162996,21.768176 19.760996,21.668175 19.359996,21.668175 17.152997,21.668175 15.346997,23.47319 15.346997,25.680208 15.346997,26.182212 15.447997,26.683216 15.647996,27.18522L13.441997,29.893242C13.340997,29.994243,13.340997,30.094244,13.240997,30.094244L13.240997,30.194244 0,30.194244 0,26.784217C0,22.671184,3.0089991,19.260156,6.9209985,18.759152z M5.8179989,9.4290762L10.933998,9.4290762 12.036997,9.4290762 14.243997,9.4290762 15.346997,9.4290762 20.061996,9.4290762 20.061996,9.530077C20.162996,9.9310799 20.162996,10.332084 20.162996,10.834087 20.162996,14.84612 16.952997,18.056146 12.939997,18.056146 8.9269981,18.056146 5.7169987,14.84612 5.7169986,10.834087 5.7169987,10.332084 5.7169987,9.9310799 5.8179989,9.4290762z M16.350996,0.80200577C18.757996,2.0060158 20.362996,4.5140362 20.362996,7.4230595 20.362996,7.8240623 20.362996,8.2260666 20.262996,8.5270691L15.046997,8.5270691C14.845997,6.520052,14.845997,1.6050129,16.350996,0.80200577z M9.7299981,0.70200539C11.535997,1.1030083,11.434998,6.4200516,11.334998,8.6270695L5.6169986,8.6270695C5.5169987,8.2260666 5.5169987,7.8240623 5.5169987,7.4230595 5.5169987,4.5140362 7.2219985,1.9060154 9.7299981,0.70200539z M12.939997,0C13.942997,0 14.945997,0.20100212 15.748997,0.602005 14.042997,2.1070175 14.343997,7.4230595 14.444997,8.6270695L11.836998,8.6270695C11.936997,7.4230595 12.237998,1.806015 10.331998,0.50200462 11.234998,0.20100212 12.036997,0 12.939997,0z"
                                          Fill="{Binding Background, Mode=OneWay}"
                                          Stretch="None"
                                          Stroke="Blue">

                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="{Binding Name, Mode=OneWay}" />
                                        </ToolTipService.ToolTip>
                                    </Path>
                                    <TextBlock HorizontalAlignment="Center"
                                               Foreground="Blue"
                                               Text="{Binding Name, Mode=OneWay}"
                                               TextAlignment="Center" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </telerik:VisualizationLayer.ItemTemplate>
                </telerik:VisualizationLayer>
            </telerik:RadMap>

 

The problem is the first Layer with the items source binding to "Jobs".
I think the Path data causes the exception.

How can I load the data asyc?

Thank you
Best regards

Markus

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 16 Jul 2020, 10:30 PM

Hello Markus,

This is a common error that appears in WPF. The single thread model of the framework doesn't allow you to manipulate the main UI thread from another thread. In your case, if the data property is modified on a background thread while the property is data bound, the reported exception will occur. To avoid this error,  you will need to schedule an update from the background thread to the UI thread. You can do this using a Dispatcher.  For example:

public void UpdateDataOnBackgroundThread()
{
	// Do calculations or whatever logic you need here.
	// Then dispatch the modifications in the Jobs collection in the dispatcher's action
	App.Current.MainWindow.Dispatcher.BeginInvoke(new Action(() =>
	{
		 // Update the Jobs collection here
		 // For example: this.Jobs = myDataResults;
		 // Or: foreach (item in myDataResults) { this.Jobs.Add(item); }
	}));
}

Regards,
Martin Ivanov
Progress Telerik

0
Markus
Top achievements
Rank 1
Veteran
answered on 17 Jul 2020, 09:19 AM

Hello Martin,

thank you for your reply. But I tried the Dispatcher before I wrote the post.
And that's not the solution. After your answer I've tried some things. 
Now I was able to isolate the problem.

It's the following XAML part in the ItemTemplate:

<Path Stroke="{Binding StrokeColor, Mode=OneWay}" 
                                  Stretch="Uniform"
                                  Fill="LightSlateGray"
                                  Data="{Binding PathData, Mode=OneWay}" >

                                <ToolTipService.ToolTip>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding CustomerName, Mode=OneWay}" FontWeight="Bold"/>
                                        <TextBlock>
                                            <Run Text="{Binding UnitNumber, Mode=OneWay}"/>
                                            <Run Text=" - "/>
                                            <Run Text="{Binding UnitType, Mode=OneWay}"/>
                                        </TextBlock>
                                        <TextBlock Text="{Binding Information, Mode=OneWay}"/>
                                        <Border BorderBrush="Black" BorderThickness="1"/>
                                        <TextBlock Text="{Binding PositionInformation, Mode=OneWay}"/>
                                    </StackPanel>
                                </ToolTipService.ToolTip>
                            </Path>

More precisely the binding to the stroke color and the path data is the problem.
If I replace the binding with a fixed color and a fixed data path, it works how expected.

Do you have any idea?

Thank you

Markus

0
Martin Ivanov
Telerik team
answered on 22 Jul 2020, 08:08 AM

Hello Markus,

In this case probably the PathData and StrokeColor properties are updated on a separate thread. You can double check this and add dispatchers also there if needed. If this doesn't help I can suggest you to open a new support ticket and attach a project the issue. 

Regards,
Martin Ivanov
Progress Telerik

0
Markus
Top achievements
Rank 1
Veteran
answered on 27 Jul 2020, 07:39 AM

Hello Martin,

unfortunately, that doesn't work either.
I think it's because it's bound to objects in a observable collection.

Now I solved the problem with triggers in XAML.

regards

Markus

0
Dilyan Traykov
Telerik team
answered on 29 Jul 2020, 01:29 PM

Hello Markus,

My name is Dilyan and I'm stepping in for my colleague Martin who is currently out of office.

If I understood correctly, you've come across a solution which works for you. If I'm mistaken in this assumption or if you require any further assistance, do not hesitate to contact us again here, or open a new support ticket if you feel you need to send over a sample project.

Regards,
Dilyan Traykov
Progress Telerik

Tags
Map
Asked by
Markus
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
Markus
Top achievements
Rank 1
Veteran
Dilyan Traykov
Telerik team
Share this question
or