I am working on a Xamarin Forms app & came across an issue where my form fields like Entry, Picker etc losing its values after resuming app from background or unlocking phone post screen timeout. I am using conventional winforms approach in my codebehind. I am not using MVVM Pattern. Please help me..
Environment is Visual Studio 2019 Community 16.6.2 with all latest packages of Xamarin Forms or Essentials etc.
Below is my code to set data to form fields.
private void FillUser()
{
var userid = PersistentObjects.UserId;
var user = new UserController().GetUserProfileById(userid);
if (user != null)
{
TxtAadhar.Text = user.AadharNumber;
TxtAchievements.Text = user.Achievements;
DdlArtField.SelectedItem = ((IList<MasterItem>)DdlArtField.ItemsSource).FirstOrDefault(c => c.Id == user.ArtDomain);
DdlLanguages.SelectedItem = ((IList<MasterItem>)DdlLanguages.ItemsSource).FirstOrDefault(c => c.Id == user.ArtLanguage);
DdlProfession.SelectedItem = ((IList<MasterItem>)DdlProfession.ItemsSource).FirstOrDefault(c => c.Id == user.ArtProfession);
DdlEducation.SelectedItem = ((IList<MasterItem>)DdlEducation.ItemsSource).FirstOrDefault(c => c.Id == user.QualificationId);
TxtMobileNum.Text = user.MobileNumber;
TxtBankAccount.Text = user.BankAccount;
DdlBanks.SelectedItem = ((IList<MasterItem>)DdlBanks.ItemsSource).FirstOrDefault(c => c.Id == user.BankId);
DdlBloodGroups.SelectedItem = user.BloodGroup;//((IList<MasterItem>)DdlBloodGroups.ItemsSource).FirstOrDefault(c => c.Name == user.BloodGroup);
TxtExp.Text = user.DomainExperience.ToString();
TxtFathersName.Text = user.FathersName;
TxtEmail.Text = user.Email.ToLower();
DdlGender.SelectedItem = user.Gender;
TxtFullName.Text = user.FullName.ToTitleCase();
TxtIfscCode.Text = user.IfscCode.ToUpper();
TxtDob.Date = user.Dob;
TxtAltMobile.Text = user.AltMobileNumber;
TxtLocality.Text = user.Locality.ToTitleCase();
DdlDistricts.SelectedItem = ((IList<MasterItem>)DdlDistricts.ItemsSource).FirstOrDefault(c => c.Id == user.DistrictId);
TxtPO.Text = user.PostOffice.ToTitleCase();
TxtVillage.Text = user.VillageName.ToTitleCase();
TxtThana.Text = user.PoliceStation.ToTitleCase();
}
}
public partial class App : Application
{
/// <summary>
///
/// </summary>
public App()
{
InitializeComponent();
OneSignal.Current.SetLogLevel(LOG_LEVEL.VERBOSE, LOG_LEVEL.NONE);
OneSignal.Current.StartInit(Constants.OSAppId).HandleNotificationOpened(NotificationOpenClicked).InFocusDisplaying(OSInFocusDisplayOption.Notification).EndInit();
Application.Current.MainPage = new Login();
}
private void NotificationOpenClicked(OSNotificationOpenedResult result)
{
PersistentObjects.ClickedNotification = true;
}
/// <summary>
///
/// </summary>
protected override void OnStart()
{
OneSignal.Current.RegisterForPushNotifications();
}
}
The form fields , having values , coming from database and is displaying in the form fields are working as expected. The form fields that are blank and needs user input are getting refreshed or emptied. I hope this will help.
If you need code of special area/operation please do let me know.
@BenReierson Comment helped me. It was a nasty mistake...I was calling api and filling data to form fields using FillUser() method, which is glued in OnAppearing(). Removing that from there worked!!!!
Thanks.