I have a question: I keep getting an InvalidOperationException: Element is already child of another element thrown in WP7. This occurs when you try to add a UIElement more then once to any display element. This would make sence, because I add multiple static members to a paragraph in a rich text box, so if I were to add the static members again, to any displayElement, I would get this exception. But in the OnNavigatingFrom method, I Clear the entire list of items in the paragraph, so they should not be in the list anymore, and the exception should not show. Other, non-static members do not show this exception. Anyone has any suggestions?
private static Hyperlink websiteHyperLink = new Hyperlink( );
method:
if ( NavigationContext.QueryString.TryGetValue( key, out value ) )
{
Bold bold = new Bold( );
bold.Inlines.Add( UsedLanguage.cv_page_website );
websiteHyperLink.Inlines.Add( website );
websiteHyperLink.TargetName = website;
websiteHyperLink.Click += new RoutedEventHandler( OpenBrowserClient );
mParagraph.Inlines.Add( bold );
mParagraph.Inlines.Add( websiteHyperLink );
mParagraph.Inlines.Add( "\n" );
}
protected override void OnBackKeyPress ( System.ComponentModel.CancelEventArgs e )
{
mParagraphs.Inlines.Clear( );
base.OnBackKeyPress ( e );
}
Don’t do this on OnBackKeyPress – this will not handle all cases.
Run your clear code in the OnNavigatedFrom() [which is what your text is saying, but not your code] override, when the NavigationMode (inside the event args) is set to “Back”.
Also, note that OnNavigateTo() can be called multiple times on the same instance (for example, when you navigate back into it) – have a local variable that says whether or not you already added the items so you don’t hit it again.
Clarification questions: Do you load the same page multiple times, or do you load it only once?