【问题标题】:WP8 xaml causes "element is already child of another element"WP8 xaml 导致“元素已经是另一个元素的子元素”
【发布时间】:2013-12-11 18:37:39
【问题描述】:

我正在按照 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();
            }
        }
    }
}
}

【问题讨论】:

  • 这……很奇怪。您能否发布堆栈跟踪,以及AppSettings 类的定义?
  • 是的,首先向我们展示 AppSettings 类 :) 即使你得到 wobbler - 你还能运行应用程序吗?您是否尝试过重建、关闭和打开解决方案/VS?
  • 问题是一个 UIElement 在同一个 UI (VisualTree) 的 2 个部分中被重用。我只是无法使用您的 AppSettings 类发生这种情况。请发布它的代码。
  • 完成 - 添加了 AppSettings 类。

标签: xaml windows-phone-8


【解决方案1】:

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

不要让 AppSettings 继承自 PhoneApplicationPage 并使其成为 ViewModel。更多信息http://msdn.microsoft.com/en-us/library/windowsphone/develop/gg521153(v=vs.105).aspx

【讨论】:

  • 这是问题的一部分。在 Microsoft 示例中,AppSettings 类不是从 PhoneApplicationPage 派生的,但该示例没有说明如何创建实际的设置页面并将其链接到 AppSettings 类。将 AppSettings 类放在页面代码中似乎是为了解决这个问题,但显然不是。如果您是 C++ 的老手,这一切都非常不同。
  • 好吧,我将类分离出来,将 Settings 页面类简化为一个构造函数,并从该页面的 xaml 中引用了新的 AppSettingsImpl 类,现在至少代码运行正常。我猜你发现了根本问题,所以我称之为已回答。
猜你喜欢
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多