'Platform' does not contain a definition for 'AppContext' Error MAUI App.

Bacardi 20 Reputation points
2023-11-26T07:38:27.0933333+00:00

I'm attempting to use the following line of code in the MainPage code behind (MainPage.xaml.cs). The project references Maui.Controls, Maui.Essentials, CommunityToolkit.MVVM, and Maui.Controls.Compatibility.

Code:

var context = Microsoft.Maui.ApplicationModel.Platform.AppContext;

VS2022 error:

CS0117: 'Platform' does not contain a definition for 'AppContext'.

AppContext is in the drop-down, however there is a yellow icon next to it. What does this mean?platform-error

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,182 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 28,876 Reputation points Microsoft Vendor
    2023-11-27T08:31:12.87+00:00

    Hello,

    Microsoft.Maui.ApplicationModel.Platform.AppContext is specific for Android platform, so you should use conditional compilation to target different platforms. Please see the following code:

    #if ANDROID
    
    
               var context = Microsoft.Maui.ApplicationModel.Platform.AppContext;
    #endif
    

    Also, you could check the source code of Microsoft.Maui.ApplicationModel.AppContext at GitHub, which targets for Android platform.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Luca Perrone 0 Reputation points
    2023-11-26T10:05:14.3666667+00:00

    The yellow icon next to it in the dropdown might indicate that there is some issue with the recognition of this property.

    Check that you have the correct namespace imported at the top of your file. I may be the IntelliSense, it might not provide accurate suggestions due to various reasons. Try cleaning and rebuilding your solution or restarting Visual Studio.

    Make sure that your packages are up to date, and the versions are compatible with each other,check the latest version: NuGet Gallery.

    Verify the version of the toolkit .MVVM is compatible with the version of MAUI