`MauiCursor`: Warning when accessing `Application.Current?.MainPage`

#441 · closed · 4 comments

View on GitHub ↗

MartyIX

This line https://github.com/VladislavAntonyuk/MauiSamples/blob/739531a109e4c95d8703faee10c4c63accea72e6/MauiCursor/CursorBehavior.cs#L11 reports the following warning in .NET 9: ``` CS0618 'Application.MainPage.get' is obsolete: 'This property has been deprecated. For single-window applications, use Windows[0].Page. For multi-window applications, identify and use the appropriate Window object to access the desired Page. Additionally, each element features a Window property, accessible when it's part of the current window. ``` However, I wonder how to fix the warning. ```diff -visualElement.SetHoverCursor((CursorIcon)newValue, Application.Current?.MainPage?.Handler?.MauiContext); +visualElement.SetHoverCursor((CursorIcon)newValue, visualElement.Window?.Handler?.MauiContext); // not possible as `visualElement.Window `is null ``` So I was thinking if one can do ```diff -visualElement.SetHoverCursor((CursorIcon)newValue, Application.Current?.MainPage?.Handler?.MauiContext); + IMauiContext? mauiContext = visualElement.Window?.Handler?.MauiContext + ?? Application.Current?.Windows[0].Handler?.MauiContext; +visualElement.SetHoverCursor((CursorIcon)newValue, mauiContext); ``` but I'm not really sure if it's a correct way to approach it. Would anyone know please? cc @VladislavAntonyuk

Comments

VladislavAntonyuk

I haven't yet updated to .NET 9, because many libraries including CommunityToolkit don't support .NET 9 either. I will try to fix all warnings after the .NET 9 release

MartyIX

I understand that the fix: ```diff -visualElement.SetCustomCursor((CursorIcon)newvalue, Application.Current?.MainPage?.Handler?.MauiContext); +visualElement.SetCustomCursor((CursorIcon)newvalue, visualElement.Handler?.MauiContext ?? Application.Current?.Windows.LastOrDefault()?.Page?.Handler?.MauiContext); ``` is meant _not_ to support multi-window applications, right? Or does it not matter from which window one gets the `MauiContext`?

VladislavAntonyuk

LastOrDefault means latest active window. But as soon as page loaded visualElement handler is not null. So first time you get contest from Last Window, then from the visual element

MartyIX

Thanks!