Background: I have an app I am migrating from Xamarin.Android to .NET MAUI Android.
What I’ve tried: I used the VS migrator tool. I’ve tried manually adding some using statements for certain AndroidX libraries.
Problem: I am getting the following errors:
Error CS0115 ‘LoginActivity.OnSaveInstanceState(Bundle)’: no
suitable method found to override (net8.0-android)Error CS0115 ‘LoginActivity.OnActivityResult(int, Result,
Intent)’: no suitable method found to overrideError CS0115 ‘LoginActivity.OnOptionsItemSelected(IMenuItem)’:
no suitable method found to override (net8.0-android)Error CS0103 The name ‘FindViewById’ does not exist in the current
context (net8.0-android)Error CS0103 The name ‘StartActivity’ does not exist in the current
context (net8.0-android)Error CS0120 An object reference is required for the non-static field, method, or property ‘Intent.HasExtra(string?)’ (net8.0-android)
Some of the code these errors reference:
protected override void OnSaveInstanceState(Bundle outState)
txtUsername = FindViewById<EditText>(Application.Context.Resources.GetIdentifier("txtUsername", "id", Application.Context.PackageName));
StartActivity(homeIntent);
clearpwd = Intent.HasExtra("clearpwd");
Question: How can I resolve these?
Where am I supposed to find information on what should be used for MAUI Android? Most documentation seems to be about cross-platform MAUI. But we are not doing that, we are doing MAUI Android (this is how Visual Studio structures it when you use the their Upgrade tool).
2
Answers
If you have a Xamarin.Android app and don’t care about other platforms then you don’t need to be concerned with MAUI. There is documentation for migrating from Xamarin.Android to .NET for Android.
It’s simple enough to do manually. Just start a new CSPROJ and copy over the
<PackageReference/>
from your old CSPROJ. 95% of the old Xamarin.Android CSPROJ will be deleted.In Maui, all projects have a
MainActivity
class(Yout project -> Platforms ->Android -> Resources -> MainActivity.cs) that derives from Activity. You can try to overrideOnSaveInstanceState
,OnActivityResult
andOnOptionsItemSelected
in it directly.Also, the
FindViewById
andStartActivity
methods are available in MainActivity.For more information about
MainActivity
in MAUI, you can refer to Generating the manifest.