Reputation: 629
I want to store object in isolated storage. I am using Application.Current.Properties
.
Problem is when I store static string to isolated storage like
Application.Current.Properties["EC"] = "10008";
then it works perfectly but when I call service (Asynchronous method) and Fill the object it doesn't store persistently.
private async Task CallWebServiceForUserProfile(string EmpCode)
{
try
{
var response = await ServiceLayer.GetResponseFromWebService.GetResponse<ServiceLayer.ServiceClasses.Profile_RootObject>
(ServiceLayer.ServiceURL.UserProfile + "UserId=" + EmpCode);
if (response != null)
{
if (response.Flag == true)
{
objProfile = new Profile();
objProfile.BranchCd = response.PrInfoList[0].BranchCd != null ? response.PrInfoList[0].BranchCd : string.Empty; ;
objProfile.CLBalance = response.PrInfoList[0].CLBalance != null ? response.PrInfoList[0].CLBalance : string.Empty;
objProfile.COFFBalance = response.PrInfoList[0].COFFBalance != null ? response.PrInfoList[0].COFFBalance : string.Empty;
objProfile.Designation = response.PrInfoList[0].Designation != null ? response.PrInfoList[0].Designation : string.Empty;
objProfile.ELBalance = response.PrInfoList[0].ELBalance != null ? response.PrInfoList[0].ELBalance : string.Empty;
objProfile.EmpCode = response.PrInfoList[0].EmpCode != null ? response.PrInfoList[0].EmpCode : string.Empty;
objProfile.EmpName = response.PrInfoList[0].EmpName != null ? response.PrInfoList[0].EmpName : string.Empty;
objProfile.Grade = response.PrInfoList[0].Grade != null ? response.PrInfoList[0].Grade : string.Empty;
objProfile.HOD = response.PrInfoList[0].HOD != null ? response.PrInfoList[0].HOD : string.Empty;
objProfile.HPLBalance = response.PrInfoList[0].HPLBalance != null ? response.PrInfoList[0].HPLBalance : string.Empty;
objProfile.LocCd = response.PrInfoList[0].LocCd != null ? response.PrInfoList[0].LocCd : string.Empty;
objProfile.ReportingTo = response.PrInfoList[0].ReportingTo != null ? response.PrInfoList[0].ReportingTo : string.Empty;
objProfile.Unit = response.PrInfoList[0].Unit != null ? response.PrInfoList[0].CLBalance : string.Empty;
objProfile.Photo = response.PrInfoList[0].Photo != null ? response.PrInfoList[0].Photo : null;
objProfile.Join_Dt = response.PrInfoList[0].Join_Dt != null ? response.PrInfoList[0].Join_Dt : string.Empty;
Application.Current.Properties["UserProfile"] = objProfile;
Application.Current.Properties.Add("UserProfile", objProfile);
Application.Current.Properties["EmployeeCode"] = response.PrInfoList[0].EmpCode != null ? response.PrInfoList[0].EmpCode : string.Empty;
//await Application.Current.SavePropertiesAsync();
}
else if (response.Flag == false)
{
await DisplayAlert(AppResources.LError, response.Message, "OK");
}
}
else
{
await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
}
}
catch (WebException Exception)
{
await DisplayAlert(AppResources.LError, AppResources.LConnectionError, "OK");
}
}
I want to use this information when my application is start.
Profile objProfile = new Profile();
if (Application.Current.Properties.ContainsKey("UserProfile"))
{
objProfile = Application.Current.Properties["UserProfile"] as Profile;
if (objProfile.EmpCode != null)
{
MyEmployeeId = objProfile.EmpCode;
}
MainPage = new SideMenu();
}
else
{
MainPage = new NavigationPage(new LoginPage());
}
As you can see in above code I have put following code to store data.
Application.Current.Properties["UserProfile"] = objProfile;
//Application.Current.Properties.Add("UserProfile", objProfile);
Application.Current.Properties["EmployeeCode"] = response.PrInfoList[0].EmpCode != null ? response.PrInfoList[0].EmpCode : string.Empty;
I have also try but don't solve my problem.
await Application.Current.SavePropertiesAsync();
Every time Login-page is open at application start.
Is there any alternative way to solve this issue? I don't want to use SQLite.
Upvotes: 0
Views: 1725
Reputation: 523
You need to serialize/deserialize your Profile object manually.
Please refer this link for example- https://codemilltech.com/persist-whatever-you-want-with-xamarin-forms/
Upvotes: 1
Reputation: 10841
Is there any alternative way to solve this issue? I don't want to use SQLite.
Store persistent data needs native API support. You can register a DependencyService for Store Settings Data and implement this service in different platforms.
For Android: You can leverage ISharedPreference to store data.
For iOS: You can use NSUserDefaults to store app settings.
Upvotes: 0