开发者

NullReferenceException in Silverlight app on Windows Phone 7

I have a Silverlight application for Windows Phone 7. This application runs fine, except when the user tombstones the app and they attempt to re-activate it. When they attempt to reactive the application, I get a NullReferenceException when I tombstone the application from one specific page. My challenge is, I cannot for the life of me determine the actual cause of this exception.

Does anyone know of any good free tools that would help me examine a potential cause? Or, does anyone have any way to determine the cause. The stack trace doesn't help as it looks like this, but doesn't identify where the problem is in my code:

   at System.Runtime.Serialization.XmlFormatReader.StoreCollectionValue(Object& collection, Object value, CollectionDataContract collectionContract)
   at System.Runtime.Serialization.XmlFormatReader.LoadValueOfCollection(DeserializedObject desObj, Object value, Int32 membersRead, Type itemType)
   at System.Runtime.Serialization.XmlFormatReader.ReadGetOnlyCollectionItem(DeserializedObject desObj, CollectionDataContract collectionContract, Int32 membersRead)
   at System.Runtime.Serialization.XmlFormatReader.ReadGetOnlyCollection(DeserializedObject desObj, CollectionDataContract collectionContract, Int32 membersRead)
   at System.Runtime.Serialization.XmlFormatReader.Deserialize(XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlFormatReader.InitializeCallStack(DataContract clContract, XmlReaderDelegator xmlReaderDelegator, XmlObjectSerializerReadContext xmlObjContext, XmlDictionaryString[] memberNamesColl, XmlDictionaryString[] memberNamespacesColl)
   at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, DataContract& dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
   at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Str开发者_JAVA技巧eam stream)
   at Microsoft.Phone.Shell.StreamPersister.Deserialize(Byte[] blob, IEnumerable`1 knownTypes, IDictionary`2& dictionary)
   at Microsoft.Phone.Shell.StreamPersister.Load(ShellPageManager shellPageManager, String key, IEnumerable`1 knownTypes, IDictionary`2& dictionary)
   at Microsoft.Phone.Shell.PhoneApplicationService.FireActivated()
   at Microsoft.Phone.Execution.NativeEmInterop.FireOnResume()

It seems like some sort of serialization issue. However, I don't know what would cause the problem. Thanks!


I had the same problem this morning. My problem was related to the use of a basic array. I changed the array to an ObservableCollection and it worked. I've detailed the problem with code on this blog.


Try to debug with a breakpoint in the Application_Activated event handler. Something could be null when you reactivate the application:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
   //insert a breakpoint here


If you suspect serialization issue, you can easily check the content of the loaded data, for example

using (var fs = isoStore.OpenFile(StoragePath, System.IO.FileMode.Open))
{
    StreamReader sr = new StreamReader(fs);
    string content = sr.ReadToEnd();
    // Put a break point here and show content variable in Xml Visualizer.
    // Can be done from Locals Debug window for example.
}

But I would start by checking your app logic. Most probably something that was not initialized yet. Should be easy to find in the debugger.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜