requestFeature() must be called before adding content in Xamarin Forms application - xamarin

I am developing one sample login application using xamarin forms, I developed code in common portable project. When running in Xamarin forms android project, this error is displayed:
Android.Util.AndroidRuntimeException: requestFeature() must be called before adding content
The code is:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init (this, bundle);
LoadApplication (new App());
}
}
How can I overcome this?

inherit your MainActivity from FormsAppCompatActivity instead of FormsApplicationActivity
https://github.com/xamarin/Xamarin.Forms/issues/13779

Related

Override Manifest file using xamarin android

Is there any possibility of the overriding manifest with a view to set at run time after the user logs in?
You could add it in your App.xaml.cs.
[Application(UsesCleartextTraffic =true)]
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Page2());
}
}

Resolution failed exception in Xamarin IOS prism

I have an iOS application written in Xamarin and I am getting a Unity Exceptions Resolution Failed exception when I try and run the application in iOS. However this error does not happen when I run the android version of the application. The exception is being thrown while the initalize function from prism is taking place.
Here is a snippet from my app.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
this.RegisterLocal(containerRegistry);
this.RegisterServices(containerRegistry);
this.RegisterPagesForNavigation(containerRegistry);
}
This code all executes and passes.
This is the iOS initialization
Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
PullToRefreshLayoutRenderer.Init();
LoadApplication(new App(new IosInitializer()));
return base.FinishedLaunching(app, options);
}
public class IosInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IUAirshipUpdate, UAirshipUpdate>();
}
}
This code also executes
The exception being thrown is an argument null exception indicating that IModuleCatelog is not present. I do not understand why it is looking for that module and can not find it. The source code on GitHub indicates that class was registered.
This issue was caused because linker behavior for IOS application was set to full and that causes issues with Unity IOC Container.

Accessing ViewModel properties from Viewclass

I want to call Content page class from Android .cs Class.
I use StartActivity(new Intent(Application.Context, typeof(LoginPage)));
showing error message
"System.ArgumentException: type Parameter name: Type is not derived
from a java type "
Does anyone know the solution for this? Please help me in Xamarin.Forms.
This should be you App.xaml.cs class
public App()
{
MainPage = new NavigationPage ( new MainPage());
}
MainActivity.cs file
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
private TextView setting;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
SetContentView(Resource.Layout.HomeScreen);
setting = FindViewById<TextView>(Resource.Id.settingText);
//check setting button is null or have value
setting.Click += delegate
{
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new LoginPage());
};
}
}
Use code above & debug to check if setting button have instance or is null.
Replace typeof(LoginPage) with LoginPage.Class or LoginPage::Class.java (kotlin)

How to register service at UWP project

I am trying to register alert service ( message dialog) at setup.cs for this method
protected override void InitializePlatformServices() {
base.InitializePlatformServices();
Mvx.RegisterSingleton<IAlertService>(new AlertService());
}
but i also have this exception http://prntscr.com/krpbcw
For services which has implementation in native projects.
Go to Setup.cs
protected override IMvxApplication CreateApp(){
Mvx.RegisterType< IAlertService, AlertService >();
}
Know one thing, AlertService implements IAlertService

Several Xamarin.Forms Android activities

I want to use several Android activities. First is general application. Second is notification view. They have different activity settings and therefore I can't use one activity for this issue.
I try to do this:
[Activity(Label = "Life Manager", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
ActionBar.SetIcon(new ColorDrawable(Color.Transparent));
LoadApplication(new TimeManagerApplication());
Device.StartTimer(new TimeSpan(0, 0, 0, 5), OpenNotificationActivity);
}
private bool OpenNotificationActivity()
{
Intent intent = new Intent(this, typeof(NotificationActivity));
StartActivity(intent);
return false;
}
}
[Activity(Label = "NotificationActivity")]
public class NotificationActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
RequestWindowFeature(WindowFeatures.NoTitle);
Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
base.OnCreate(savedInstanceState);
Forms.Init(this, savedInstanceState);
LoadApplication(new NotificiationApplication());
}
}
In this line:
LoadApplication(new NotificiationApplication());
I take an error:
System.NullReferenceException: Object reference not set to an instance of an object.
How can I use two android activities in one application and use cross-platform Xamarin.Forms views for it?
Update:
Without these lines application perfectly works:
//RequestWindowFeature(WindowFeatures.NoTitle);
//Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
But how can I hide status bar and use fullscreen view?
I decided this issue.
To take Fullscreen view without status bar is enough use only Window Flags
Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.KeepScreenOn);
without:
RequestWindowFeature(WindowFeatures.NoTitle);
Application works as expected without exceptions.

Resources