Error Reading Serialized Object? WP7
I am reading an object from Isolated Storage with the following code:
public static T Load<T>(string name) where T : class, new()
{
T loadedObject = null;
using (IsolatedStorageFile开发者_如何转开发 storageFile = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream storageFileStream = new IsolatedStorageFileStream(name, System.IO.FileMode.OpenOrCreate, storageFile))
{
if (storageFileStream.Length > 0)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
loadedObject = serializer.ReadObject(storageFileStream) as T; //####Error Here####
}
if (loadedObject == null)
{
loadedObject = new T();
}
}
Upon reading my object I am getting a SecurityException which reads
"The type 'Microsoft.Xna.Framework.Media.Song' cannot be deserialized in partial trust because it does not have a public parameterless constructor."
My object class being read contains a Song property which is throwing the above error.
Is there some way around this? I would like for my Song property to be stored with my object. Any advice would be greatly appreciated! Thanks!
What is your goal?
- Do you want to just play a song in the app?
- Do you create your song in the app?
- Do you want to have access to the song library?
- Do you want to get the song from the web?
@1 Just add a song to your conent in your project. no need to use serialization.
@2 Use memory stream to mainulate it. save it and then load the stream. Use MediaElement.SetSource(Stream stream)
@3 Use MediaPlayerLauncher
@4 Song song = Song.FromUri..
You need to store it in a diffrent way. Exception explatins pretty much all why a Song is a bad choice in this case.
Look at this example. http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/3080021c-3960-49a0-ac5b-ebf2680592e1
精彩评论