Xamarin Forms overriding OnAppearing causes app to crash


Matt Douhan

I am trying to do a simple override and load some data when my page loads, I am using the following code in the code behind page.

namespace XYZ
{
    public partial class MainPage : ContentPage
    {
        private Label results;
        private Label groupResults;

        public MainPage()
        {
            InitializeComponent();

            results = new Label();

            groupResults = new Label();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            storeIdTxt.Text = Settings.StoreIdSetting;
        }
}
}

If I uncomment the override things works just fine, the error I am getting seems to be a generic one attached here

Error Message

my settings class is fairly simple as follows

using System;
using System.Collections.Generic;
using System.Text;
using Plugin.Settings;
using Plugin.Settings.Abstractions;

namespace NWMPosNG.Helpers
{
  /// <summary>
  /// This is the Settings static class that can be used in your Core solution or in any
  /// of your client applications. All settings are laid out the same exact way with getters
  /// and setters. 
  /// </summary>
  public static class Settings
{
    private static ISettings AppSettings
    {
        get
        {
            return CrossSettings.Current;
        }
    }

    #region Setting Constants

    private const string SettingsKey = "settings_key";
    private static readonly string SettingsDefault = string.Empty;

    private const string StoreId = null;
    private static readonly string StoreIdDefault = "0";

    #endregion


    public static string GeneralSettings
        {
            get
                {
                    return AppSettings.GetValueOrDefault(SettingsKey, SettingsDefault);
                }

            set
                {
                    AppSettings.AddOrUpdateValue(SettingsKey, value);
                }
        }

    public static string StoreIdSetting
        {
            get
            {
                return AppSettings.GetValueOrDefault(StoreId, StoreIdDefault);
            }

            set
            {
                AppSettings.AddOrUpdateValue(StoreId, value);
            }
        }
}
}

I narrowed down the issue to when I access the saved data using

storeIdTxt.Text = Settings.StoreIdSetting;

But I don't understand why that causes the crash.

Trevi Awater

You are using the Settings Plugin from James Montemagno. Which is pretty much a KeyValuePair that is stored on the local device across sessions.

In your case:

AppSettings.GetValueOrDefault(StoreId, StoreIdDefault); 

Translates to:

AppSettings.GetValueOrDefault(null, "0");

Which crashes because 'null' can't be a key. That's why setting the key (StoreId) prevents the crash from happening.

Articles connexes


Que se passe-t-il dans OnAppearing of Xamarin Forms?

Alan2 J'ai une application Forms qui fonctionne bien mais je remarque que les couleurs d'arrière-plan apparaissent incorrectement pendant une fraction de seconde lorsque la page apparaît. J'ai ce code pour mon OnAppearing protected override async void OnAp

Xamarin.Forms App crash on iOS after updating to 13.1

Tharindu I was using Visual Studio 2017 with Visual Studio 2017 on mac. Mono version was 5.18.1 and Xamarin.iOS version was 12.4 (I think). The app worked fine without any problem. Recently I have updated Visual Studios on both Windows and Mac to 2019 and Xama

Xamarin.Forms App crash sur iOS après la mise à jour vers 13.1

Tharindu J'utilisais Visual Studio 2017 avec Visual Studio 2017 sur mac. La version mono était 5.18.1 et la version Xamarin.iOS était 12.4 (je pense). L'application a bien fonctionné sans aucun problème. Récemment, j'ai mis à jour Visual Studios sur Windows et

Alternative à base.OnAppearing dans Xamarin.Forms

stephen.webb Questions de base, existe-t-il une alternative à base.OnAppearing qui ne fonctionnera qu'une seule fois? Actuellement, dans OnAppearing, j'ai un script qui génère des cadres en fonction du résultat d'une requête. Si je verrouille l'écran et que je

DisplayAlert causing application to crash in Xamarin.Forms

A. Sinha When ever I try to put an alert message on any page in my application, the application crashes just before displaying the alert in Xamarin.Forms. async void OnAlertYesNoClicked (object sender, EventArgs e) { var answer = await DisplayAlert ("Questio

Xamarin.Forms Page.DisplayAlert dans Page.OnAppearing

Bondoline Je ne parviens pas à DisplayAlertafficher une fenêtre contextuelle dans le OnAppearingrappel d'un Xamarin.Forms Page. Voici ce que j'ai essayé jusqu'à présent : protected override void OnAppearing() { base.OnAppearing(); this.DisplayAlert("A

SensorManager.registerListener causes app to crash

Ramy670 The app has no problems until I add the very last line, and when it is commented out, the app works again: class MainActivity : AppCompatActivity() { private lateinit var manager: SensorManager private lateinit var accelerometer: Sensor p