c#


List does not get saved when I use Application.Current.SavePropertiesAsync in Xamarin forms


I am currently trying to save a list in Xamarin forms. I use this code snippet:
var list = new List<myClass> ();
Application.Current.Properties["myList"] = list;
await Application.Current.SavePropertiesAsync();
When I then close the app and use this code...
if (Application.Current.Properties.ContainsKey("myList"))
{
}
...I cannot reach this if statement.
I read something about people having issues saving a list with this solution but they solved it by converting it to a string. I am a bit unsure on how to do this. If I do this...
Application.Current.Properties["myList"] = list.ToString();
...the app crashes.
I saw that there is a plugin called "Settings" that I might need to use instead in case there isn't a solution to this problem but I would prefer to work with my current code if possible.
The Properties dictionary can only serialize primitive types for
storage. Attempting to store other types (such as List can
fail silently).
It means that you can't save List because it's not a primitive type of object. You can serialize it to JSON string for example then it will work. Code example:
var jsonValueToSave = JsonConvert.SerializeObject(myList);
Application.Current.Properties["myList"] = jsonValueToSave;
await Application.Current.SavePropertiesAsync();
Don't forget to deserialize JSON to List<string> when loading the value back.
Note that yourList.ToString() will not work in this case. More info here.
P.S.: Get familiar with the official documentation & check this post on another related thread.

Related Links

Why a ToList embedded in a WHERE clause behaves differently than a ToList executed outside of the query?
Transferring values from one form to another
Get Cells position with SmartXLS workbook
Multiple signalr hubs with different configuration
Calling ServiceController.WaitForStatus in another method returns immediately
Calling static method from static method would be thread safe in C#?
Is it possible to get list of query names that references particular iteration path
how to determine the weekend days in oracle table?
Cannot implicitly convert type 'long' to “int?”?
c# Windows Phone 8.1 Install Twitterizer using Package Manager Console
ASP.NET MVC application throwing system.nullreferenceexception - modelbinding to list [duplicate]
Facebook SDK .Net - Error Authenticating, Calling App ID Does not match
How to return data culled from multiple multiselect listboxes in a C#/SQL Server environment
An unhandled exception of System.InvalidOperationException breaks my MVC App?
Crystal Report page split up and encrypting the generated document in C#
c# Simple TASK Cancel

Categories

HOME
actionscript-3
spring-boot
primefaces
bitbucket
crash
opc-ua
google-docs-api
download
ms-access-2010
wavefront
restheart
activejdbc
osrm
expo
aggregate
openstack-horizon
reportlab
samba
ofbiz
cucumberjs
restructuredtext
beaker-notebook
functional-dependencies
grunt-contrib-uglify
oracle10g
android-6.0-marshmallow
typeclass
bytebuffer
git-submodules
units-of-measurement
vtd-xml
boost-log
login-required
zimbra
sampling
device-driver
jslider
julia-jump
ui-grid
binary-decision-diagram
colorbox
text-extraction
rivets.js
cloudconvert
jcifs
android-vectordrawable
cc
blackberry-webworks
distcc
cherry-pick
nimbus
razorengine
phpdesigner
salesforce-service-cloud
transport
pushwoosh
syncano
consul-template
p4python
glog
skview
applinks
teamstudio-unplugged
gnip
disabled-control
debuggervisualizer
mixradio
tlbimp
google-checkout
brew
biztalk-deployment
boost.build
dnsbl
cryptarithmetic-puzzle
wpml
esi
event-propagation
filedialog
advanceddatagrid
yii-cactiverecord
flexbuilder
backlight
jqtransform
noise-reduction
table-valued-parameters
iphone-maps
large-data-volumes
appointment
tracd

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App