maui icon indicating copy to clipboard operation
maui copied to clipboard

[regression/8.0.3] Issue with android application context

Open JamesBrooksbankIceland opened this issue 1 year ago • 1 comments

Description

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: var con = Android.App.Application.Context

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.

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.

Images attached show the items in .net 7 and .net 8 net7

net8

(Post originally on Microsoft Learn on following url https://learn.microsoft.com/en-us/answers/questions/1434565/issue-with-android-application-context-in-net-8)

Steps to Reproduce

  1. Create a new .net8 maui application
  2. Make it for Android only
  3. In any part of the code use the code line "var con = Android.App.Application.Context"
  4. Examine the base context of the variable con

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.49

Affected platforms

Android

Affected platform versions

Android 11

Did you find any workaround?

No

Relevant log output

No response

JamesBrooksbankIceland avatar Nov 23 '23 08:11 JamesBrooksbankIceland

Has there been any update on this, some sort of feedback would be appreciated.

JamesBrooksbankIceland avatar Jan 05 '24 09:01 JamesBrooksbankIceland

After not hearing from anything on here, i decided to try using other methods which Microsoft have referenced, including Platform.Context and using the MauiContext.

The same problem occurs in .net for both

JamesBrooksbankIceland avatar Jan 19 '24 13:01 JamesBrooksbankIceland

Verified this issue with Visual Studio 17.10.0 Preview 1; Maui Version 8.0.6, I add the code in MauiProgram.cs file, I guess the issue was not repro now. image If my steps are incorrect, please let me know. I'll retest it.

QianaJiao avatar Mar 08 '24 09:03 QianaJiao

Problem still occurs using Maui 8.0.7, Visual Studio 17.8.7

We have found workarounds to get the context but this is still an issue which should be looked at.

Screenshot 2024-03-08 100005

JamesBrooksbankIceland avatar Mar 08 '24 10:03 JamesBrooksbankIceland

Verified again, this issue does repro on .NET 8, and is fine on .NET 7. (Maui 8.0.7, Visual Studio 17.10.0 Preview 1) .NET 8: image .NET 7: image

QianaJiao avatar Mar 11 '24 09:03 QianaJiao

The problem not it is Net 8, the problem are nuget packets, Microsoft.Maui.Controls vers 8.0.7 and Microsoft.Maui.Controls.Compability 8.0.7 are good, but if you update to 9.0.0 then errors happens

Thorastur avatar Mar 19 '24 13:03 Thorastur

And i want you tranlate this to the correspnd department. In Windows 10, net 8.0.3 not working, at least the version installed by VS. Try this, install VS with Net 8.0.3 the open console and try the commands of NET, for example "dotnet --info" you can see not work and VS not work with 8.0.3. I don´t know in win 11. At least in my PC not working and it is causing i am 5 days with headeach and can´t work, and yet i don´t have a working VS vesion properly installed and working... have a nice day!!!

Thorastur avatar Mar 19 '24 13:03 Thorastur

Problem still occurs using Maui 8.0.7, Visual Studio 17.8.7

We have found workarounds to get the context but this is still an issue which should be looked at.

What is your workaround to get the context if you don't mind sharing?

indyjason79 avatar Apr 06 '24 11:04 indyjason79

We discovered that within the library where we needed the application context, it would accept the Main Activity instance as this is was a type of Activity which inherited from the ContextThemeWrapper and seems to contain the properties which we needed in order for us to proceed.

var currentActivity = (MainActivity)Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; return currentActivity;

So we where fortunate that we could work with the Activity object rather than just the context. May not be best for the application, but may be.

JamesBrooksbankIceland avatar Apr 08 '24 07:04 JamesBrooksbankIceland

Hi, i have created a sample application using the maui template

https://github.com/JamesBrooksbankIceland/ContextIssueExample

You can access this and if you view MainPage.xaml.cs, when the app is running and the button is clicked, place a debug point on line 25, when run, hover over the con variable to see the problem

JamesBrooksbankIceland avatar Jun 06 '24 11:06 JamesBrooksbankIceland