Issue with android application context in .net 8

James Brooksbank 20 Reputation points
2023-11-22T11:49:52.0866667+00:00

Hi, so i am working on modifying an existing maui app which is working fine in .net7, however there is a problem with the version in .net8.

After examining both instances i have determined that there is a difference between the 2 versions, which is the only difference.

In both versions i am using the code line at the bottom.

In .net7, when i examine con, i can drill down into the base and see that it is "Microsoft.Maui.MauiApplication}"

I can examine all the other properties within that without issue.

.net7

However, in .net8, with the same line, you can see the base, but underneath that, all you see is the message "object reference not set to an instance of an object "

Everything else is the same, however there is something wrong in the initialisation of the MauiApplication which i believe is causing this due to .net8.

Can someone help me resolving this issue.

.net8

var con = Android.App.Application.Context
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,207 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,936 Reputation points Microsoft Vendor
    2023-11-23T05:55:46.1733333+00:00

    Hello,

    I tried to use following code, I can get the same issue.

    #if ANDROID
                var res1 = Microsoft.Maui.MauiApplication.Context;
                var context = Platform.AppContext;           
                var reS2=   Android.App.Application.Context;
    #endif
    

    Q&A is not a bug reporting or tracking system. Please report it in the Maui github repository at https://github.com/dotnet/maui . We will clear details to be able to reproduce and troubleshoot the problem. When you post it, please include version numbers, exact repro steps, and the difference between the expected behaviour and actual behaviour.

    By the way, if you want to get current Activity, you can use var CurrentActivity= Microsoft.Maui.ApplicationModel.Platform.CurrentActivity;, it is working as normal in .NET 8

    Best Regards,

    Leon Lu


    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.


0 additional answers

Sort by: Most helpful