You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am migrating from Xamarin.Forms to .NET MAUI and I need to initialize a component when the activity is created (OnCreate). In Xamarin.Fomrs you used LoadApplication and also IPlatformInitializer. Here in .NET MAUI you can't use LoadApplication and also IPlatformInitializer. Replacement for IPlatformInitializer is to register the RegisterTypes in MauiProgram.cs. There I use UsePrism and follow the instructions in this documentation. After that I still can't figure out how to initialize this component. I also tried .NET MAUI builder.Services.AddTransient but it didn't work.
This is my AndroidInitializer where I Initialize my component in RegisterTypes
public class AndroidInitializer : IAndroidInitializer {
public MainActivity MainActivity { get; }
public AndroidInitializer() {
}
public AndroidInitializer(MainActivity mainActivity) {
MainActivity = mainActivity;
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance(typeof(INotificationService), new AndroidNotificationService(Android.App.Application.Context, MainActivity));
}
}
This is my IAndroidInitializer where I define the RegisterTypes, which is kinda just a copy of IPlatformInitializer from Xamarin.Forms.
public interface IAndroidInitializer
{
void RegisterTypes(IContainerRegistry containerRegistry);
}
This is my MainActivity where I call OnCreate and also where in Xamarin.Forms the LoadApplication was located.
public class MainActivity : MauiAppCompatActivity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
new AndroidInitializer(this);
}
//also tried doing this
//protected override void OnResume() {
// base.OnResume();
// Platform.OnResume();
// new AndroidInitializer(this);
// }
//protected override void OnNewIntent(Intent intent) {
// base.OnNewIntent(intent);
//
// Platform.OnNewIntent(intent);
//}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I am migrating from Xamarin.Forms to .NET MAUI and I need to initialize a component when the activity is created (
OnCreate
). In Xamarin.Fomrs you usedLoadApplication
and alsoIPlatformInitializer
. Here in .NET MAUI you can't useLoadApplication
and alsoIPlatformInitializer
. Replacement forIPlatformInitializer
is to register theRegisterTypes
in MauiProgram.cs. There I useUsePrism
and follow the instructions in this documentation. After that I still can't figure out how to initialize this component. I also tried .NET MAUIbuilder.Services.AddTransient
but it didn't work.This is my MauiProgram.cs code
This is my AndroidInitializer where I Initialize my component in RegisterTypes
This is my
IAndroidInitializer
where I define theRegisterTypes
, which is kinda just a copy ofIPlatformInitializer
from Xamarin.Forms.This is my MainActivity where I call OnCreate and also where in Xamarin.Forms the
LoadApplication
was located.Can you help me with this?
Thank you,
Anzeko
Beta Was this translation helpful? Give feedback.
All reactions