beto-rodriguez / LiveCharts2

Simple, flexible, interactive & powerful charts, maps and gauges for .Net, LiveCharts2 can now practically run everywhere Maui, Uno Platform, Blazor-wasm, WPF, WinForms, Xamarin, Avalonia, WinUI, UWP.
https://livecharts.dev
MIT License
4.39k stars 573 forks source link

LiveCharts Navigation Exception on iOS and .NET 9 #1703

Open jai-io opened 1 week ago

jai-io commented 1 week ago

Describe the bug When navigating away from a page containing a LiveChart graph using the back button, the application throws the following casting exception:

System.Exception: Unable to cast to ContentView
   at LiveChartsCore.SkiaSharpView.Maui.ChartBehaviour.<>c__DisplayClass0_0.<On>b__0(Object sender, EventArgs e) in /_/src/skiasharp/LiveChartsCore.SkiaSharpView.Maui/ChartBehaviour.cs:line 58

To Reproduce Steps to reproduce the behavior:

  1. Load a page containing a LiveChart graph
  2. Navigate away from the page using the back button

Environment:

Additional context While I haven't thoroughly investigated the Android platform, preliminary observations suggest this issue is also present there.

sharifxu commented 5 days ago

i'm experiencing the same issue with 2.0.0-rc2, any workarounds for this?

xhorack commented 3 days ago

The same for me. Exceptionis on all platforms, Win, iOS, Android. On Android there is 'Unable to cast to ContentViewGroup' exception. This happened only after upgrading Microsoft.Maui.Controls to version 9.0.10 from 8 on net9. So i'm not sure if this is addresed to MS or LiveCharts. Also tried latest rc 4.5 of LiveCharts, but behavior is the same. After spending a couple of endless nigths, i have still no workaround.

jai-io commented 2 days ago

Looks like this is due to changes in .NET 9 in the way custom control using handlers are cleaned up: Handler disconnection.

Adding HandlerProperties.DisconnectPolicy="Manual" to the control seems to fix the issue. As an example:

                    <maui:CartesianChart
                        Grid.Column="1"
                        HandlerProperties.DisconnectPolicy="Manual"
                        HeightRequest="52"
                        Margin="-16,0,0,0"
                        Series="{Binding Source={RelativeSource AncestorType={x:Type report:StockReportSummaryViewModel}}, Path=StockSummaryPrices.Series}"
                        XAxes="{Binding Source={RelativeSource AncestorType={x:Type report:StockReportSummaryViewModel}}, Path=StockSummaryPrices.XAxes}"
                        YAxes="{Binding Source={RelativeSource AncestorType={x:Type report:StockReportSummaryViewModel}}, Path=StockSummaryPrices.YAxes}"
                        TooltipPosition="Hidden" />