title: WP8 xaml causes "element is already child of another element"
body: <p>I am following MSDN examples to add a settings page to my first Windows Phone 8 application (warning - I am completely new to XAML, I'm a C++ guy).</p>
  
  <p>The xaml looks like this:</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage
  x:Class="PicoSDU.AppSettings"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:PicoSDU"
  FontFamily="{StaticResource PhoneFontFamilyNormal}"
  FontSize="{StaticResource PhoneFontSizeNormal}"
  Foreground="{StaticResource PhoneForegroundBrush}"
  SupportedOrientations="Portrait" Orientation="Portrait"
  mc:Ignorable="d"
  shell:SystemTray.IsVisible="True"&gt;
  
  
  &lt;phone:PhoneApplicationPage.Resources&gt;
     &lt;local:AppSettings x:Key="PicoSettings"&gt;&lt;/local:AppSettings&gt;
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  
  
  &lt;!--LayoutRoot is the root grid where all page content is placed--&gt;
  &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;!--TitlePanel contains the name of the application and page title--&gt;
      &lt;StackPanel Grid.Row="0" Margin="12,17,0,28"&gt;
          &lt;TextBlock Text="PicoSDU" Style="{StaticResource PhoneTextNormalStyle}"/&gt;
          &lt;TextBlock Text="Settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/&gt;
      &lt;/StackPanel&gt;
  
      &lt;!--ContentPanel - place additional content here--&gt;
      &lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;
  
  
       &lt;StackPanel Margin="30,0,0,0"&gt;
          &lt;TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtIpAddress" Text="IP Address" VerticalAlignment="Top" Width="169" /&gt;
          &lt;TextBox Name="tbIpAddress"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                   Text="{Binding Source={StaticResource PicoSettings}, Path=IpSetting, Mode=TwoWay}"/&gt;
          &lt;TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtPort" Text="Port Number" VerticalAlignment="Top" Width="169" /&gt;
          &lt;TextBox Name="tbPort"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                   Text="{Binding Source={StaticResource PicoSettings}, Path=PortSetting, Mode=TwoWay}"/&gt;
          &lt;TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtSysId" Text="System ID" VerticalAlignment="Top" Width="169" /&gt;
          &lt;TextBox Name="tbSysId"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                   Text="{Binding Source={StaticResource PicoSettings}, Path=SysIdSetting, Mode=TwoWay}"/&gt;
          &lt;TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtWsId" Text="Station ID" VerticalAlignment="Top" Width="169" /&gt;
          &lt;TextBox Name="tbWsId"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                   Text="{Binding Source={StaticResource PicoSettings}, Path=WsIdSetting, Mode=TwoWay}"/&gt;
       &lt;/StackPanel&gt;
  
    &lt;/Grid&gt;
  &lt;/Grid&gt;
  
  &lt;/phone:PhoneApplicationPage&gt;
  </code></pre>
  
  <p>So, pretty simple. four text boxes. It rendered perfectly OK until I added the resource clause</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage.Resources&gt;
    &lt;local:AppSettings x:Key="PicoSettings"&gt;&lt;/local:AppSettings&gt;
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  </code></pre>
  
  <p>As soon as I add that the XAML parser throws a wobbler and the root PhoneApplicationPage gets the old blue squiggly and reports our favourite "element is already the child of another element" error. If I remove the resource clause, that error goes away and the xaml renders, but of course the textbox bindings then all throw an error because they cannot see their resources.</p>
  
  <p>I've been googling this the last three hours and I can't see what's wrong, and none of the answers I've found here and elsewhere seem to fit. Can some kind soul show me the blindingly stupid thing I've done and please put me out of my misery?</p>
  
  <p><b>Edit</b>
  Here's the AppSettings class. It's just the Microsoft code sample, hacked into the code-behind:</p>
  
  <pre><code>namespace PicoSDU
  {
  public partial class AppSettings : PhoneApplicationPage
  {
      // Our settings
      IsolatedStorageSettings settings;
  
      // The key names of our settings
      const string IpSettingKeyName    = "IpSetting";
      const string SysIdSettingKeyName = "SysIdSetting";
      const string WsIdSettingKeyName  = "WsIdSetting";
      const string PortSettingKeyName  = "PortSetting";
  
      // The default value of our settings
      const string IpSettingDefault    = "81.179.24.51";
      const string SysIdSettingDefault = "1";
      const string WsIdSettingDefault  = "511";
      const string PortSettingDefault  = "1846";
  
      public AppSettings()
      {
         InitializeComponent ();
         try
         {
            settings = IsolatedStorageSettings.ApplicationSettings;
         }
         catch (System.IO.IsolatedStorage.IsolatedStorageException e)
         {
            // handle exception
         }
      }
  
      public bool AddOrUpdateValue(string Key, Object value)
      {
          bool valueChanged = false;
  
          // If the key exists
          if (settings.Contains(Key))
          {
              // If the value has changed
              if (settings[Key] != value)
              {
                  // Store the new value
                  settings[Key] = value;
                  valueChanged = true;
              }
          }
          // Otherwise create the key.
          else
          {
              settings.Add(Key, value);
              valueChanged = true;
          }
         return valueChanged;
      }
  
      public T GetValueOrDefault&lt;T&gt;(string Key, T defaultValue)
      {
          T value;
  
          // If the key exists, retrieve the value.
          if (settings.Contains(Key))
          {
              value = (T)settings[Key];
          }
          // Otherwise, use the default value.
          else
          {
              value = defaultValue;
          }
          return value;
      }
  
      public void Save()
      {
          settings.Save();
      }
  
      public string IpSetting
      {
          get
          {
              return GetValueOrDefault&lt;string&gt;(IpSettingKeyName, IpSettingDefault);
          }
          set
          {
              if (AddOrUpdateValue(IpSettingKeyName, value))
              {
                  Save();
              }
          }
      }
  
      public string SysIdSetting
      {
         get
         {
            return GetValueOrDefault&lt;string&gt; ( SysIdSettingKeyName, SysIdSettingDefault );
         }
         set
         {
            if (AddOrUpdateValue ( SysIdSettingKeyName, value ))
            {
               Save ();
            }
         }
      }
  
      public string WsIdSetting
      {
         get
         {
            return GetValueOrDefault&lt;string&gt; ( WsIdSettingKeyName, WsIdSettingDefault );
         }
         set
         {
            if (AddOrUpdateValue ( WsIdSettingKeyName, value ))
            {
               Save ();
            }
         }
      }
  
      public string PortSetting
      {
          get
          {
             return GetValueOrDefault&lt;string&gt; ( PortSettingKeyName, PortSettingDefault );
          }
          set
          {
             if (AddOrUpdateValue ( PortSettingKeyName, value ))
              {
                  Save();
              }
          }
      }
  }
  }
  </code></pre>