In my app.xaml.cs I create a new page.
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new WrapLayoutPage());
}
This page calls a static class, which uses the DependencyService to perform some tasks.
The line which throws the error:
var tmpTable = SqLiteHelper.GetItem<TableX>("someId");
SqLiteHelper:
public static class SqLiteHelper
{
private static readonly SQLiteConnection DatabaseConnection = DependencyService.Get<ISqLite>().GetConnection();
private static readonly object Locker = new object();
public static DbObjectV3 GetItem<T>(Guid inId) where T : DbObjectV3, new()
{
lock (Locker)
{
var tmpItem = DatabaseConnection.Table<T>().FirstOrDefault(inItem => inItem.Id == inId);
tmpItem.IsNewObject = false;
return tmpItem;
}
}
}
This throws me a TypeInitializationException with the InnerException:
You MUST call Xamarin.Forms.Init(); prior to using it
It's somehow related to the static helper class, because prior to that call, I can use the DependencyService without any problems!
As mainlauncher I'm using a splash screen. In this class I do some startup work, which relies on the DependencyService.
SplashScreen:
[Activity(Theme = "#style/MyTheme.Splash", NoHistory = true, MainLauncher = true)]
public class SplashScreen : Activity
{
static readonly string TAG = "X:" + typeof(SplashScreen).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
}
My MainActivity:
[Activity(Label = "FrameworkForms", Icon = "#drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "#style/MainActivityTheme", MainLauncher = false)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
App.ScreenWidth = (double)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
LoadApplication(new App());
}
}
Now after changing the Activity in SplashScreen to FormsAppCompatActivity, I get another error.
Call Forms.Init() before Hide Keyboard
What's the thing here?
This is pretty unfortunate. I used the wrong OnCreate() method in my SplashScreen.
I changed SplashScreen to:
[Activity(Theme = "#style/MyTheme.Splash", NoHistory = true, MainLauncher = true)]
public class SplashScreen : Activity
{
static readonly string TAG = "X:" + typeof(SplashScreen).Name;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Forms.Forms.Init(this, savedInstanceState);
Log.Debug(TAG, "SplashActivity.OnCreate");
}
protected override void OnResume()
{
base.OnResume();
Task tmpStartupWork = new Task(() =>
{
Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
StartUpTasks.InitializeDatabaseCreation();
Log.Debug(TAG, "Working in the background - important stuff.");
});
tmpStartupWork.ContinueWith(inT =>
{
Log.Debug(TAG, "Work is finished - start MainActivity.");
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}, TaskScheduler.FromCurrentSynchronizationContext());
tmpStartupWork.Start();
}
}
Unfortunately, the documentation on Xamarin about creating a splash screen uses the OnCreate() method with 2 parameters!
I also came across this mistake. The cause of the error was that I wanted to change my namespace. From
public partial class AppDelegate :
global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
I changed it to:
using Xamarin.Forms.Platform.iOS;
public partial class AppDelegate : FormsApplicationDelegate
Then I saw the following:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
I thought, it would be useful to adjust that as well... without thinking more exactly about it. And I wrote:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
this.Init();
A while later, I got the mistake and had to laboriously search for the cause. Finally I found and fixed it. Maybe it can help someone :-) Cheers...
Related
I'm trying to handle a notifications opening on android that I've sent through Google's FCM on the SharedProject level by using "Plugin.FirebasePushNotification" plugin and subscribing to "OnNotificationOpened" event.
When I try to open the notification while the app is in the background I'm able to hit a breakpoint inside "OnNotificationOpened" event but after the work inside the event ends the App const. is hit and the application restarts.
I want the application to continue from its last state and hit "OnResume" method that is located inside the App class.
I've done some research about it online and almost all of them were about how splash activity might cause this issue so I've disabled my splash activity by turning the whole "SplashActivity.cs" file into a comment except the namespace and usages and implemented the wanted features of splashactivity(style) inside the "MainActivity" class after that I made sure my "MainActivity" class was the only activity that had it's "MainLauncher" attribute set to true but the problem continues.
Here are the relevant classes:
FirebaseReg.cs
public class FirebaseReg
{
public static void FirebaseInit()
{
CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
{
System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
CrossFirebasePushNotification.Current.Subscribe("defaultTopic");
};
CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
try
{
//Breakpoint hits here and I'm able to finish all my work without any exceptions
}
catch (Exception ex)
{
}
};
CrossFirebasePushNotification.Current.OnNotificationAction += (s, p) =>
{
};
CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;
}
private static void Current_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs e)
{
//DO WORK
}
}
App.xaml.cs
public partial class App : Application
{
public static Uri ServerUri = new Uri(" ");
public static bool IsInForeground { get; set; } = false;
public App()
{
try
{
InitializeComponent(); //After the work in OnNotificationOpened ends breakpoint goes here
Application.Current.UserAppTheme = OSAppTheme.Unspecified;
Application.Current.MainPage = new LoginPage();
}
catch (Exception ex)
{
}
}
protected override void OnStart()
{
IsInForeground = true;
}
protected override void OnSleep()
{
IsInForeground = false;
}
protected override void OnResume()
{
IsInForeground = true;
}
}
MainActivity.cs
[Activity(Label = "TestProject1", Theme = "#style/MyTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.Window.RequestFeature(WindowFeatures.ActionBar);
// Name of the MainActivity theme you had there before.
// Or you can use global::Android.Resource.Style.ThemeHoloLight
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(savedInstanceState);
Rg.Plugins.Popup.Popup.Init(this);
UserDialogs.Init(this);
Xamarin.FormsGoogleMaps.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
FirebasePushNotificationManager.ProcessIntent(this ,Intent);
LoadApplication(new App());
}
protected override void OnNewIntent(Intent intent)
{
FirebasePushNotificationManager.ProcessIntent(this, intent);
base.OnNewIntent(intent);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
MainApplication.cs
[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
FirebasePushNotificationManager.DefaultNotificationChannelId = "DefaultChannel";
FirebasePushNotificationManager.DefaultNotificationChannelName = "defaultTopic";
}
#if DEBUG
FirebasePushNotificationManager.Initialize(this, true);
FirebaseRegister.FirebaseInit();
#else
FirebasePushNotificationManager.Initialize(this, false);
FirebaseRegister.FirebaseInit();
#endif
}
public override void OnTerminate()
{
base.OnTerminate();
UnregisterActivityLifecycleCallbacks(this);
}
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityDestroyed(Activity activity)
{
}
public void OnActivityPaused(Activity activity)
{
}
public void OnActivityResumed(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityStopped(Activity activity)
{
}
}
From the Android docs, it states this:
If it has declared its launch mode to be "multiple" (the default) and
you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it
will be finished and re-created; for all other launch modes or if
FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to
the current instance's onNewIntent().
That's why your app is restarting.
The launchMode attribute of the activity affects how the activity is launched.
singleTop, singleTask, or singleInstance should be used to prevent the notification intent from creating a new activity instance.
The flag FLAG_ACTIVITY_NEW_TASK doesn't influence a new activity being created, but makes the launched activity the root of a new task.
For more, check:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
https://developer.android.com/guide/components/activities/tasks-and-back-stack
I added a public method to return variable pageLoaded but visual studio shows this error "An object reference is needed by the method MainPage.PageLoaded() not static". The logic is to finish Splash Activiy only if pageLoaded be true. If someone has a better idea for this, would be nice to know, I'm just started learning C#/Xamarin.
My code:
namespace MyApp
{
public partial class MainPage : ContentPage
{
private bool pageLoaded = false;
public MainPage()
{
InitializeComponent();
webnav.HeightRequest = 1000;
webnav.WidthRequest = 1000;
webnav.Source = "https://www.example.com";
}
public void Webnav_Navigated(object sender, WebNavigatedEventArgs e)
{
pageLoaded = true;
}
public bool PageLoaded()
{
return pageLoaded;
}
}
}
Code 2:
...
...
using MyApp;
namespace MyApp.Droid
{
[Activity(Label = "My App", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
while (true)
{
if (MainPage.PageLoaded())
{
SplashScreen.fa.Finish();
break;
}
}
}
}
}
Problem(s):
Your .PageLoaded() method is an instance method. It can only be called on an instantiated object of type MainPage
Your reference to it, in your splash screen, is attempting to call it as a static method which has two problems:
As stated above, you didn't define it using the static keyword
Even if you define it static, it won't tell you anything about a real page being loaded
Solution:
Don't do this. Control the visibility of your splash screen from the instance of the page that knows when it has been loaded. Create and reference the splash screen from within MainPage
Edit:
To further clarify the difference:
// calling a method against the static definition of the class
MainPage.PageLoaded();
// calling a method against an instance of the class
new MainPage().PageLoaded();
The above code is not a solution but rather an example of the difference between an instance method and a static method. Your PageLoaded method should not be static as you've laid everything out so far.
Since MainPage class is not static, you need to create an object of MainPage class and use that object to call the method from that class.
I am new in xamarin ios, my app is crashed when running the app and throws an exception "Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Could not load NIB in …".How can I manage it..
Please help me...
Here is my code ViewController
public partial class LoginViewController : UIViewController
{
public LoginViewController(IntPtr handle) : base(handle)
{
}
public LoginViewController() :base("LoginViewController",null)
{
}
public override void DidReceiveMemoryWarning()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
#region View lifecycle
public override void ViewDidLoad()
{
base.ViewDidLoad();
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
}
partial void Btn_Login_TouchUpInside(UIButton sender)
{
}
#endregion
}
here is my AppDelegate
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
UIWindow window;
UINavigationController navigationController;
UIViewController viewController;
window = new UIWindow(UIScreen.MainScreen.Bounds);
viewController = new LoginViewController();
navigationController = new UINavigationController();
navigationController.PushViewController(viewController, false);
window.RootViewController = navigationController;
window.MakeKeyAndVisible();
return true;
}
I got an exception in Main.cs
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
try
{
UIApplication.Main(args, null, "AppDelegate");
}
catch(System.Exception ex)
{
}
}
}
Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Could not load NIB in …
If you initalize viewController using viewController = new LoginViewController(); , and the class have the constructors
public LoginViewController() :base("LoginViewController",null) , it will look for .xib file when running, but Visual Studio removed the template of viewController with xib since 15.7, you must create viewcontroller with storyboard, so the error occurs.
You can follow this tutorial to implement the way like before.
But we strongly recommend you to create viewcontroller as below.
Workaround 1
Create a new class inherit from UIViewController(just a single .cs)
[Register("LoginViewController")]
public class LoginViewController : UIViewController
{
public LoginViewController()
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.View.BackgroundColor = UIColor.Red;
// Perform any additional setup after loading the view
}
}
But in this way you have to manage UI programmatically.
Workaround 2
Create viewController with Storyboard, you can drag ViewController from Toolbox to the designer , specify the Class and provide Storyboard ID.
In AppDelegate modify the way that initalizing the viewcontroller.
//viewController = new LoginViewController();
viewController = UIStoryboard.FromName("LoginViewController",null).InstantiateViewController("LoginViewController");
A little thing
Window is global variable in AppDelegate and used in the whole application, you should never create a local variable UIWindow ,just use the default Window.
I've just started looking into Xamarin and just can not to wrap around my head how to make multiple Activities have a reference same instance of service.
I am starting KeyPressedReceiver from MainActivity and start listening for power button being pressed.
When three click are being made, I am calling service method InitCancelActivity, which starts playing mp3 file and opens CancelActivity.
In CancelActivity there is a text field and a button. And when user press this button, I want the value from text field to be passes to the GeneralService method KillAlert.
The question is how to reference instance of GeneralService (which is already created) from CancelActivity, so I could call KillAlert?
And this part
if (_service == null)
_service = new GeneralService();
looks absolutely wrong. Should I instantiate it in MainActivity and pass to a KeyPressedReceiver constructor?
[Activity(Label = "TTTT", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
KeyPressedReceiver receiver;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
receiver = new KeyPressedReceiver();
RegisterReceiver(receiver, new IntentFilter(Intent.ActionScreenOn));
}
}
[BroadcastReceiver(Enabled = true)]
public class KeyPressedReceiver : BroadcastReceiver
{
private GeneralService _service;
private int _clicks = 0;
public override void OnReceive(Context context, Intent intent)
{
if (_service == null)
_service = new GeneralService();
_clicks++;
if (_clicks > 5)
{
_service.InitCancelActivity();
}
}
}
[Service(Name = "com.ff.GeneralService")]
public class GeneralService : Service {
private readonly Android.Media.MediaPlayer _player;
public GeneralService()
{
_player = new Android.Media.MediaPlayer();
}
public void RaiseAlert()
{
// start playing .mp3 file
}
public void KillAlert(string pass)
{
// stop playing .mp3 file
}
public void InitCancelActivity()
{
this.RaiseAlert();
var i = new Intent(this, typeof(CancelActivity));
i.SetFlags(ActivityFlags.NewTask);
this.StartActivity(i);
}
}
[Activity(Label = "CancelActivity")]
public class CancelActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.cancel);
this.FindViewById(Resource.Id.cancelButtonYes).Click += delegate
{
var password = this.FindViewById(Resource.Id.cancelPassword);
// call KillAlert method from GeneralServic
};
}
}
Create static GeneralService instance and use in Cancel Activity.
for example
[Service(Name = "com.ff.GeneralService")]
public class GeneralService : Service {
private readonly Android.Media.MediaPlayer _player;
public static generalService;
public GeneralService()
{
_player = new Android.Media.MediaPlayer();
generalService=this
}
public void RaiseAlert()
{
// start playing .mp3 file
}
public void KillAlert(string pass)
{
// stop playing .mp3 file
}
public void InitCancelActivity()
{
this.RaiseAlert();
var i = new Intent(this, typeof(CancelActivity));
i.SetFlags(ActivityFlags.NewTask);
this.StartActivity(i);
}
}
and use in CancelActivity like below example
[Activity(Label = "CancelActivity")]
public class CancelActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.cancel);
this.FindViewById(Resource.Id.cancelButtonYes).Click += delegate
{
var password = this.FindViewById(Resource.Id.cancelPassword);
// call KillAlert method from GeneralServic
GeneralService.generalService.KillAlert(password.TEXT);
};
}
}
I have a simple wifi app. It is just scanning wifis and listing them. I figured out my ScanResult List is filling in another method but in OnPrepare/CreateOptionsMenu it is always null.
This method calls first when program starts I know that, but when i try to call it again with InvalidateOptionsMenu(); nothing changes. Here is my piec of code:
MainActivity:
public class MainActivity : Activity
{
public static Context context;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button wifiButton = FindViewById<Button>(Resource.Id.popupButton);
RegisterReceiver(new WiFiReceiver(), new IntentFilter(WifiManager.ScanResultsAvailableAction));
((WifiManager)GetSystemService(WifiService)).StartScan();
wifiButton.Click += (s, arg) => {
InvalidateOptionsMenu();
PopupMenu menu = new PopupMenu(this, wifiButton);
menu.Inflate(Resource.Menu.menu);
menu.Show();
};
}
public override bool OnCreateOptionsMenu(IMenu menu) {
WiFiReceiver wifiReceiver = new WiFiReceiver();
IList<ScanResult> availableWifis = wifiReceiver.GetWifiList();//This is always null
if (availableWifis != null) {
foreach (ScanResult token in availableWifis) {
menu.Add(token.Ssid);
}
}
return base.OnCreateOptionsMenu(menu);
}
}
WiFi Scanning this class:
class WiFiReceiver : BroadcastReceiver {
private IList<ScanResult> wifiList;
private string message;
public override async void OnReceive(Context context, Intent intent) {
MainActivity mainActivity = (MainActivity)context;
WifiManager wifiManager = (WifiManager)mainActivity.GetSystemService(Context.WifiService);
this.message = string.Join("\r\n", wifiManager.ScanResults
.Select(r => $"{r.Ssid} - {r.Level} dB"));
this.wifiList = wifiManager.ScanResults.ToList(); //This has connection informations
mainActivity.DisplayText(message);
await Task.Delay(TimeSpan.FromSeconds(1));
wifiManager.StartScan();
}
public IList<ScanResult> GetWifiList() {
return wifiList;
}