4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidでXamarin FormsのApplication.Current.Propertiesが保存されない

Posted at

問題

Androidで、Application.Current.Propertiesに保存した値がアプリを再起動すると復元されない。
デバッグビルドでは正常に復元されるが、リリースビルドのときだけダメ。
iOSでは正常に復元される。

実行時ログにこのように出ている:

Xamarin.Forms PropertyStore: Exception while reading Application properties: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00011] in <8b2a39fe03af4d18bd69fa49d25dc113>:0 
  at System.ThrowHelper.ThrowArgumentOutOfRangeException () [0x00000] in <8b2a39fe03af4d18bd69fa49d25dc113>:0 
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x0000c] in <8b2a39fe03af4d18bd69fa49d25dc113>:0 
  at System.Runtime.Serialization.XmlFormatReaderInterpreter.StoreCollectionValue (System.Object collection, System.Type valueType, System.Object value, System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00032] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlFormatReaderInterpreter.ReadCollection (System.Runtime.Serialization.CollectionDataContract collectionContract) [0x001e4] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlFormatReaderInterpreter.ReadCollectionFromXml (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Runtime.Serialization.XmlObjectSerializerReadContext context, System.Xml.XmlDictionaryString itemName, System.Xml.XmlDictionaryString itemNamespace, System.Runtime.Serialization.CollectionDataContract collectionContract) [0x00025] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlFormatReaderGenerator+CriticalHelper+<GenerateCollectionReader>c__AnonStorey1.<>m__0 (System.Runtime.Serialization.XmlReaderDelegator xr, System.Runtime.Serialization.XmlObjectSerializerReadContext ctx, System.Xml.XmlDictionaryString inm, System.Xml.XmlDictionaryString ins, System.Runtime.Serialization.CollectionDataContract cc) [0x0000c] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Runtime.Serialization.XmlObjectSerializerReadContext context) [0x0004e] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlReaderDelegator reader) [0x00000] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize (System.Runtime.Serialization.XmlReaderDelegator reader, System.String name, System.String ns, System.Type declaredType, System.Runtime.Serialization.DataContract& dataContract) [0x00282] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Type declaredType, System.Runtime.Serialization.DataContract dataContract, System.String name, System.String ns) [0x0000e] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.DataContractSerializer.InternalReadObject (System.Runtime.Serialization.XmlReaderDelegator xmlReader, System.Boolean verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x0013e] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions (System.Runtime.Serialization.XmlReaderDelegator reader, System.Boolean verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x0007a] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions (System.Runtime.Serialization.XmlReaderDelegator reader, System.Boolean verifyObjectName) [0x00000] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00007] in <60b7cf293a0e42789c2b4630bbb58b98>:0 
  at Xamarin.Forms.Platform.Android.Deserializer+<>c.<DeserializePropertiesAsync>b__1_0 () [0x0003b] in <95895bcb439e42ebb8e7134a545f1acc>:0 

解決法

https://forums.xamarin.com/discussion/64410/in-android-linker-options-have-to-chose-the-dont-link-option-or-the-propertystore-wont-work
に書いてあるが、要はXamarin Formsのバグというよりリンカの問題で、ワークアラウンドは2つある。

  1. Androidプロジェクトのオプションで「リンクしない」を選択する
  2. 同じくオプションで「無視するアセンブリ」にSystem.Runtime.Serializationを追加する

1か2のどちらかを行えば良い。
当方では2のみ実施してうまくいくことを確認した。

image

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?