WP8 xaml导致“元素已经是另一个元素的子元素”

时间:2013-12-10 22:49:41

标签: xaml windows-phone-8

我正在关注MSDN示例,将设置页面添加到我的第一个Windows Phone 8应用程序中(警告 - 我是XAML的新手,我是C ++人员。)

xaml看起来像这样:

<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">


<phone:PhoneApplicationPage.Resources>
   <local:AppSettings x:Key="PicoSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>


<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="PicoSDU" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="Settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">


     <StackPanel Margin="30,0,0,0">
        <TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtIpAddress" Text="IP Address" VerticalAlignment="Top" Width="169" />
        <TextBox Name="tbIpAddress"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                 Text="{Binding Source={StaticResource PicoSettings}, Path=IpSetting, Mode=TwoWay}"/>
        <TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtPort" Text="Port Number" VerticalAlignment="Top" Width="169" />
        <TextBox Name="tbPort"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                 Text="{Binding Source={StaticResource PicoSettings}, Path=PortSetting, Mode=TwoWay}"/>
        <TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtSysId" Text="System ID" VerticalAlignment="Top" Width="169" />
        <TextBox Name="tbSysId"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                 Text="{Binding Source={StaticResource PicoSettings}, Path=SysIdSetting, Mode=TwoWay}"/>
        <TextBlock Height="36" HorizontalAlignment="Left" Margin="0,0,0,0" Name="txtWsId" Text="Station ID" VerticalAlignment="Top" Width="169" />
        <TextBox Name="tbWsId"  HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="274"  
                 Text="{Binding Source={StaticResource PicoSettings}, Path=WsIdSetting, Mode=TwoWay}"/>
     </StackPanel>

  </Grid>
</Grid>

</phone:PhoneApplicationPage>

所以,非常简单。四个文本框。在我添加资源子句

之前,它完全正常
<phone:PhoneApplicationPage.Resources>
  <local:AppSettings x:Key="PicoSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>

一旦我添加XAML解析器抛出一个摇摆器,根PhoneApplicationPage就会得到旧的蓝色波浪形,并报告我们最喜欢的“元素已经是另一个元素的子元素”错误。如果我删除了资源子句,那么该错误就会消失并且xaml呈现,但是当然文本框绑定都会引发错误,因为它们无法看到它们的资源。

过去三个小时我一直在谷歌搜索,我看不出有什么问题,我在这里和其他地方找到的答案似乎都不合适。某种善良的灵魂能否向我展示我所做过的令人眼花缭乱的愚蠢事情,请让我摆脱困境?

修改的 这是AppSettings类。这只是微软的代码示例,入侵了代码隐藏:

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<T>(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<string>(IpSettingKeyName, IpSettingDefault);
        }
        set
        {
            if (AddOrUpdateValue(IpSettingKeyName, value))
            {
                Save();
            }
        }
    }

    public string SysIdSetting
    {
       get
       {
          return GetValueOrDefault<string> ( SysIdSettingKeyName, SysIdSettingDefault );
       }
       set
       {
          if (AddOrUpdateValue ( SysIdSettingKeyName, value ))
          {
             Save ();
          }
       }
    }

    public string WsIdSetting
    {
       get
       {
          return GetValueOrDefault<string> ( WsIdSettingKeyName, WsIdSettingDefault );
       }
       set
       {
          if (AddOrUpdateValue ( WsIdSettingKeyName, value ))
          {
             Save ();
          }
       }
    }

    public string PortSetting
    {
        get
        {
           return GetValueOrDefault<string> ( PortSettingKeyName, PortSettingDefault );
        }
        set
        {
           if (AddOrUpdateValue ( PortSettingKeyName, value ))
            {
                Save();
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

你的代码非常棒。您正在尝试将一个页面(您的AppSettings类继承自PhoneApplicationPage)嵌入到另一个页面中。一种更好的方法是使用MVVM模式。

不要让AppSettings继承PhoneApplicationPage并将其变成ViewModel。有关详情,请访问http://msdn.microsoft.com/en-us/library/windowsphone/develop/gg521153(v=vs.105).aspx