-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
New Bug in .NET 8: GetVisualTreeElements(point) is now broken and won't run at all, says "MauiContext should have been set on parent." #18070
Comments
Your example works fine. But I know what it takes to get that error to happen. Border border = new();
border.StrokeShape = new Microsoft.Maui.Controls.Shapes.RoundRectangle() { CornerRadius = 10 };
border.BackgroundColor = Colors.Green; Add a shape, and it'll throw. Because of #16919. So I'm 99% sure in the context of your application, you probably have a shape or some element without a handler, which will cause this to throw. The API for GetVisualTreeElements hasn't changed since it was introduced, AFAIK. The reason it's throwing now is because of These added border shapes to the logical children list (which is correct). But shapes (or potentially other objects) don't have handlers, and the underlying logic for getting the platform view assumes they do (because they should, it's listed as non-nullable) which is why it's broken now. #16919 needs to be fixed to fix this issue. e. Also, as a heads up, unless you're using this for debug tooling, I would not use that API in a real-world application. It's intended for Visual Diagnostics tooling and is not designed to be efficient in a real-world usecase. Depending on what you're doing, there are probably other ways to do what you need that would be better. |
You are right, it is the Border that is doing it. If I comment out:
...the error stops showing. I do not need to add a Stroke Shape as you suggest. I presume this means the Border already has a default Stroke Shape if that is the source of the problem. Or perhaps we are using different versions if you don't have the bug on my demo project? For me, I can just open the project, play it in .NET 8.0.0-rc.1.9171 and click on screen and get that error with the project exactly as written in my OP. Happens in Windows and Android for sure at least every time. As for the performance of this function, to my knowledge there is nothing that replaces it although I may have to write my own version by copying/pasting/editing the code for it eventually. I am overriding all hit detection functions with an overlay layer similar to The only other problem with this function is it does not behave as I would like for detecting "invisible" objects. If you don't add a background color to layout elements, for example, it goes straight through them and they are not detected. I need to test this more but it is certainly a problem on Windows for me. Any further thoughts are appreciated, but at this point just getting the Border glitch fixed is critical as now this function is completely unusable in .NET 8.0 unless there are no Borders on screen. Thanks for your feedback and work on it. I appreciate at least knowing where it's coming from. |
Duplicate of #16919 |
Description
Previously,
IVisualTreeElement.GetVisualTreeElements(point)
fromMicrosoft.Maui.VisualTreeElementExtensions
worked, allowing us to get a list of elements at a given point. This was fine in .NET 7.However, I upgraded to .NET 8 to try to benefit from other bug fixes and this no longer works. Now any attempt to run it results in the error
MauiContext should have been set on parent.
Is there any way this could be fixed?
Steps to Reproduce
Open the repro, play project, click anywhere on screen to run the function, get the bug error result in .NET 8 but not .NET 7.
Code replacing App.xaml.cs in default project:
Link to public reproduction project repository
https://github.com/jonmdev/Get-Visual-Tree-Bug
Version with bug
8.0.0-rc.1.9171
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
7.0.96
Affected platforms
iOS, Android, Windows
Affected platform versions
All .NET 8
Did you find any workaround?
Nope.
Relevant log output
No response
The text was updated successfully, but these errors were encountered: