none
PhotoChooserTask - Type 'System.Windows.UIElement' cannot be serialized RRS feed

  • Question

  • I have a page that launches a PhotoChooserTask with Show(). After the show, and before the photo chooser is displayed, an Application_UnhandledException is thrown.

    "Type 'System.Windows.UIElement' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."

    The stack starts with ...
    at Microsoft.Phone.Shell.PhoneApplicationService.FireDeactivated()
    at Microsoft.Phone.Execution.NativeEmInterop.FireOnPause()

    It appears tombstoning cannot serilize something on that page. I also call this page from a panorama page in a different part of the app without problems.

    Is there a way to figure out what it is choaking on? Any work around?
    Thursday, October 28, 2010 2:51 AM

Answers

  • Alright then. Type 'System.Windows.UIElement' cannot be serialized means just that. Even if you have stored UI controls into temp storage via PhoneApplicationService.Current.State[KeyName].

    I have a page that uses joost's swipetitle page to create a pivot like page that can handle controls with lateral gestures... maps, sliders, multi touch controls. As the visible 'page' went out of view I stored that area of the page to PhoneApplicationService.Current.State. It worked great in reducing memory but tombstoning did not like those controls in PhoneApplicationService.Current.State.

    Thanks to Mick for his patience.
    Saturday, October 30, 2010 4:38 AM

All replies

  • Thanks for the links. good stuff.

    I am not persisting any UI during tombstoning. I am actually not persisting anything. The exception happens after the PhotoChooserTask Show() and after it has cleared OnNavigatingFrom().

    The exception Call Stack starts with...
    at Microsoft.Phone.Execution.NativeEmInterop.FireOnPause()

    So presumably it is the WP7 OS saving off the screen??

    I can post the call stack if it will help. Any Ideas?
    Thursday, October 28, 2010 5:32 AM
  • Can this be reproduced in an empty project? Presumably there's something in your NavigatedFrom that would need to be migrated to the repro project to produce this error? Hard to say without seeing the code that causes it.. as far as I'm aware PhotoChooserTask itself is working ok under other conditions.
    Thursday, October 28, 2010 5:37 AM
  • No it can't be reproduced in an empty project. This very code works fine in other parts of the app. The only difference I know is that when it fails it is deeper in the app.

    It works fine for pano->detail page->photo chooser
    It fails for pano->page with user control->detail page->photo chooser

    Is there a limit for number of pages that can be tombstoned?

    Here is the code that launches.
    public partial class EditPeep : PhoneApplicationPage
    {
    PhotoChooserTask objPhotoChooser;

    public EditPeep()
    {
    InitializeComponent();
    objPhotoChooser = new PhotoChooserTask();
    objPhotoChooser.Completed += new EventHandler(objPhotoChooser_Completed);
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
    base.OnNavigatingFrom(e);
    if (e.NavigationMode == NavigationMode.Back)
    { App.TempSave("navPreviousPage", "PeepEdit"); }
    }

    private void btnPictureChooser_Click(object sender, RoutedEventArgs e)
    {
    objPhotoChooser.Show();
    }
    }

    Here is the call stack
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(Type type)
    at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type)
    at System.Runtime.Serialization.ClassDataContract..ctor(Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(Type type)
    at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type)
    at System.Runtime.Serialization.ClassDataContract..ctor(Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(Type type)
    at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type)
    at System.Runtime.Serialization.ClassDataContract..ctor(Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.DataContract.GetDataContract(RuntimeTypeHandle typeHandle, Type type, SerializationMode mode)
    at System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(RuntimeTypeHandle typeHandle, Type type)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at System.Runtime.Serialization.XmlFormatWriter.InternalSerialize(MethodInfo methodInfo, Object memberValue, Type memberType, Boolean writeXsiType, XmlObjectSerializerWriteContext context, XmlWriterDelegator xmlWriter)
    at System.Runtime.Serialization.XmlFormatWriter.WriteValue(Type memberType, Object memberValue, Boolean writeXsiType, XmlObjectSerializerWriteContext context, XmlWriterDelegator xmlWriter)
    at System.Runtime.Serialization.XmlFormatWriter.WriteMember(SerializingObject serObj, Int32 memberIndex, ClassDataContract derivedMostClassContract)
    at System.Runtime.Serialization.XmlFormatWriter.WriteClass(CallStackElement`1 callStackElement)
    at System.Runtime.Serialization.XmlFormatWriter.Serialize(XmlObjectSerializerWriteContext context)
    at System.Runtime.Serialization.XmlFormatWriter.InitializeCallStack(XmlWriterDelegator xmlWriterDel, Object obj, XmlObjectSerializerWriteContext writeContext, DataContract contract)
    at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)
    at Microsoft.Phone.Shell.StreamPersister.Serialize(IDictionary`2 dictionary, IEnumerable`1 knownTypes)
    at Microsoft.Phone.Shell.StreamPersister.Save(ShellPageManager shellPageManager, String key, IDictionary`2 dictionary, IEnumerable`1 knownTypes)
    at Microsoft.Phone.Shell.PhoneApplicationService.FireDeactivated()
    at Microsoft.Phone.Execution.NativeEmInterop.FireOnPause()
    Friday, October 29, 2010 1:45 AM
  • What do you see for memory consumption when deeper in the app before/after tombstoning?

    (DeviceTotalMemory, ApplicationCurrentMemoryUsage, ApplicationPeakMemoryUsage)

    Device Information for Windows Phone


    Also wondered if an empty app with just "pano->page with user control->detail page->photo chooser" in it's essence in a blank project would repro the issue. Idea being to see if some combination of using these features somehow leads to the problem.
    Friday, October 29, 2010 2:02 AM
  • Pano->detail->photo chooser | Pano->page with user control->detail->photo chooser
    Before After | Before
    Device total: 390012928 390012928 | 390012928
    Memory: 39264256 41508864 | 46870528
    Peak: 40423424 41586688 | 47894528

    I confirmed that it throws the same exception if I launch PhotoChooserTask, EmailAddressChooserTask, or CameraCaptureTask. And it also fails if I launch a chooser from a diffrent page on that same level... pano-> page with user controls-> detail -> launcher.

    I am not sure how to pull out and isolate the pieces that are causing a problem because there is so much. I was hoping someone knew a pattern to issolate which controls might be the problem from the stack or other housing keeping tasks?
    Friday, October 29, 2010 4:17 AM
  • Doesnt look like you're hitting any memory walls.

    I dont know exactly what you're doing with the user control, but the repro I had in mind was on the assumption that it was somehow playing a part in the navigation path by way of either directing the navigation or presenting the page that houses the control that fires the navigation. The thought was just to repro a path through a pano and user control in the same way in a blank project.. not pulling over any of the substance. Just to see if the pattern in use was somehow broken. It's speculatuive, but probably fairly easy.
    Friday, October 29, 2010 4:32 AM
  • The user controls are not in the navigation path. They contain lists, maps, and other user controls. They do navigate to other pages. But pages that launch the choosers are being navigated to from menu bar button events.

    I created another app that has the same navigation path as the pages that fail... pano -> page with user control -> detail page -> photo chooser. It works fine.

    I'm not sure what to do next. The exception appears to originate out side of my app, in the WP7 OS tombstoning code? Are there any tools that might provide better insight into the error? Or is there another forum or source that might have a better understanding of the tombstoning internals?

    I can't provide any choosers at that level in the app... where they are most needed.
    Friday, October 29, 2010 6:28 AM
  • So the approach I've been taking is to try and isolate what will make the difference to whether it works or not.

    If known, this would result in something concrete that can lead to a resolution of some form.

    The only other thing I see from your latest that might help in an attempt to narrow down the source of the conflict is the map mention. If this is the silverlight map control do you get the same behaviour in your main and repro app without / with this present?

    I suspect I'm going to be out of specific ideas to try and narrow it down after this. You may be able to see more looking at your source with a similar approach.

    Or perhaps just hope someone reads it and has seen the issue before and either recognises it or has nutted it out.

    Friday, October 29, 2010 6:48 AM
  • Ok so I added a map to the repo app and it works fine. And I remove the map from the 'real' app and it fails as before.

    So to recap the problem as I see it. I have a page (actually more than one ) that launches a photo chooser (or other choosers) and works fine. When I use that exact page in a different part of the app I recieve the "Type 'System.Windows.UIElement' cannot be serialized " exception. After tha app is tombstoned.

    So it appears tombstoning is serializing the entire app not just incrementally serializing the currently open page, because that is not different than before. And it is then failing on a specific UI element not on the current page and not a Map.

    Continuing to 'nut it out' but this forum is moving pretty fast and I wanted to bump this to the top in hopes that someone reads this and has seen this issue before.
    Saturday, October 30, 2010 1:40 AM
  • Alright then. Type 'System.Windows.UIElement' cannot be serialized means just that. Even if you have stored UI controls into temp storage via PhoneApplicationService.Current.State[KeyName].

    I have a page that uses joost's swipetitle page to create a pivot like page that can handle controls with lateral gestures... maps, sliders, multi touch controls. As the visible 'page' went out of view I stored that area of the page to PhoneApplicationService.Current.State. It worked great in reducing memory but tombstoning did not like those controls in PhoneApplicationService.Current.State.

    Thanks to Mick for his patience.
    Saturday, October 30, 2010 4:38 AM
  • No worries, glad you worked it out.

    Saving (serialisable) data is all you want to do as you discovered. That may not have been clear from my first post.
    Saturday, October 30, 2010 4:42 AM