Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiveCharts Navigation Exception on iOS and .NET 9 #1703

Open
jai-io opened this issue Nov 17, 2024 · 8 comments
Open

LiveCharts Navigation Exception on iOS and .NET 9 #1703

jai-io opened this issue Nov 17, 2024 · 8 comments
Labels
bug Something isn't working
Milestone

Comments

@jai-io
Copy link

jai-io commented Nov 17, 2024

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:

  • OS: Mac OS 15.1 (24B83)
  • iOS Simulator: 18.1
  • .NET SDK: 9.0.100
  • LiveChartsCore.SkiaSharpView.Maui Nuget Package: 2.0.0-rc4.5

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

@jai-io jai-io changed the title LiveCharts Navigation Exception on iOS LiveCharts Navigation Exception on iOS and .NET 9 Nov 17, 2024
@sharifxu
Copy link

sharifxu commented Nov 20, 2024

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

@xhorack
Copy link

xhorack commented Nov 22, 2024

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
Copy link
Author

jai-io commented Nov 23, 2024

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" />

@xhorack
Copy link

xhorack commented Nov 26, 2024

Well, you are right, i can confirm on all plaforms everything works fine. Thank you.

@jo-ruch
Copy link

jo-ruch commented Dec 9, 2024

This happens on Maui 8 on Windows as well when calling Graph.Handler?.DisconnectHandler().

Maui 8.0.21
net8.0-windows10.0.19041.0
LiveChartsCore.SkiaSharpView.Maui 2.0.0-rc4.5

274|2024-12-09T13:27:35.3479033+00:00|ERROR|1|VesselHomePage|Threw --> System.Exception: Unable to cast to ContentPanel
   at LiveChartsCore.SkiaSharpView.Maui.ChartBehaviour.<>c__DisplayClass0_0.<On>b__0(Object sender, EventArgs e) in /_/src/skiasharp/LiveChartsCore.SkiaSharpView.Maui/ChartBehaviour.cs:line 74
   at Microsoft.Maui.Controls.Element.OnHandlerChangedCore()
   at Microsoft.Maui.Controls.VisualElement.OnHandlerChangedCore()
   at Microsoft.Maui.Controls.View.OnHandlerChangedCore()
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler()
   at Slick.Views.Components.TelemetryGraph.OnDestroy()

Project to replicate the issue: https://github.com/jo-ruch/LiveCharts2MauiMemoryLeakDemo/tree/disconnect_handler_exception

@maonaoda
Copy link

This might be a problem with the implementation of LiveCharts.
Currently, if HandlerProperties.DisconnectPolicy="Manual" is not added, Maui will automatically Disconnect.
At this time, the HandlerChanged event set in ChartBehaviour will be triggered.
At this time, element.Handler?.PlatformView is already null

            var contentView = (Microsoft.Maui.Platform.ContentView?)element.Handler?.PlatformView
                ?? throw new System.Exception("Unable to cast to ContentView");

@maonaoda
Copy link

maonaoda commented Dec 16, 2024

The correct approach should be to use Handler (ConnectHandler/DisconnectHandler)
instead of HandlerChanged

I'll try to modify this process when I have some free time.

@beto-rodriguez
Copy link
Owner

beto-rodriguez commented Dec 26, 2024

I tested and I was able to reproduce on the master branch (v rc-4.5)

The good news is that this was fixed with #1704 by @albyrock87, now the chart uses the ChartViewHandler to connect and disconnect handlers, this is now merged to dev and will be included in the next version of the library.

I think this could also fix #1725

@beto-rodriguez beto-rodriguez added the bug Something isn't working label Dec 26, 2024
@beto-rodriguez beto-rodriguez added this to the rc-5 milestone Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants