简体   繁体   中英

Tombstone wp7 how save Enum… and other unkwon object

When I save a variable that containing an Enum type in wp7 (C#) I receive an Exception, the meaning is that enum is a not known type so the system cannot serialize.

for example

public enum videoType:int {
       LongVideo=1,
      ShortVideo }

or

public enum video
{

       LongVideo,ShortVideo
}


 _videoType = videoType.ShortVideo 


PhoneApplicationService.Current.State["myType"]

someone tell me to use Datacontract and data member but seems not available in wp7

so how save a enum type?

You will have to save the Enum Value as integer and reset to correct Enum value from the integer after the page gets activated.

[Read back the saved integer value and set the correct enum value. Lookup Enum.Parse/Enum.TryParse]

Also you can have a look at the EnumValueToDescription Converter attribute implemented by Josh Smith in his article here http://www.codeproject.com/KB/WPF/WPFJoshSmith.aspx?msg=3766336 [WPF]

http://www.michaelsnow.com/2010/12/25/how-to-convert-an-enum-to-its-string-value/

DataContract and DataMember most definitely are available in all versions of WP7.

You do not need to specify int as the ancestor type because int is the default ancestor type.

public enum videoType  
{
  LongVideo = 1,
  ShortVideo, 
}

Since you can convert int to an int-based enum with simple cast, I suggest casting as int to save and casting back to your enum to load.

请参见以下答案: 如何将枚举值序列化为int?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM