WP8 xaml原因“;元素已经是另一个元素的子元素; 我在MSDN的例子中添加了一个设置页面到我的第一个Windows Phone 8应用程序(警告我是XAML全新的,我是C++的家伙)。

WP8 xaml原因“;元素已经是另一个元素的子元素; 我在MSDN的例子中添加了一个设置页面到我的第一个Windows Phone 8应用程序(警告我是XAML全新的,我是C++的家伙)。,xaml,windows-phone-8,Xaml,Windows Phone 8,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;assemb

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>

所以,很简单。四个文本框。在我添加resource子句之前,它表现得非常好

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

只要我添加XAML解析器抛出一个抖动,根PhoneApplicationPage就会出现一个蓝色的扭曲,并报告我们最喜欢的“元素已经是另一个元素的子元素”错误。如果我删除resource子句,错误就会消失,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();
            }
        }
    }
}
}
名称空间PicoSDU
{
公共部分类AppSettings:PhoneApplicationPage
{
//我们的设置
隔离存储设置;
//设置的关键名称
常量字符串IpSettingKeyName=“IpSetting”;
常量字符串SysIdSettingKeyName=“SysIdSetting”;
常量字符串WsIdSettingKeyName=“WsIdSetting”;
常量字符串PortSettingKeyName=“PortSetting”;
//设置的默认值
常量字符串IpSettingDefault=“81.179.24.51”;
常量字符串SysIdSettingDefault=“1”;
常量字符串WsIdSettingDefault=“511”;
常量字符串PortSettingDefault=“1846”;
公共应用程序设置()
{
初始化组件();
尝试
{
设置=隔离存储设置。应用程序设置;
}
catch(System.IO.IsolatedStorage.IsolatedStorageException e)
{
//处理异常
}
}
public bool AddOrUpdateValue(字符串键,对象值)
{
bool valueChanged=false;
//如果密钥存在
if(设置.包含(键))
{
//如果值已更改
如果(设置[键]!=值)
{
//存储新值
设置[键]=值;
valueChanged=true;
}
}
//否则,创建密钥。
其他的
{
设置。添加(键、值);
valueChanged=true;
}
返回值已更改;
}
公共T GetValuerDefault(字符串键,T defaultValue)
{
T值;
//如果键存在,则检索该值。
if(设置.包含(键))
{
值=(T)设置[键];
}
//否则,使用默认值。
其他的
{
值=默认值;
}
返回值;
}
公共作废保存()
{
设置。保存();
}
公共字符串设置
{
得到
{
返回GetValueOrDefault(IpSettingKeyName,IpSettingDefault);
}
设置
{
if(AddOrUpdateValue(IpSettingKeyName,值))
{
Save();
}
}
}
公共字符串系统设置
{
得到
{
返回GetValueOrDefault(SysIdSettingKeyName,SysIdSettingDefault);
}
设置
{
if(AddOrUpdateValue(SysIdSettingKeyName,值))
{
保存();
}
}
}
公共字符串WsIdSetting
{
得到
{
返回GetValueOrDefault(WsIdSettingKeyName,WsIdSettingDefault);
}
设置
{
if(AddOrUpdateValue(WsIdSettingKeyName,值))
{
保存();
}
}
}
公共字符串端口设置
{
得到
{
返回GetValueOrDefault(PortSettingKeyName,PortSettingDefault);
}
设置
{
if(AddOrUpdateValue(PortSettingKeyName,值))
{
Save();
}
}
}
}
}

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


不要将AppSettings从PhoneApplicationPage继承并设置为ViewModel。更多信息请访问

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


不要将AppSettings从PhoneApplicationPage继承并设置为ViewModel。更多信息请访问

,这是。。。奇怪的你能发布堆栈跟踪,也许还有
AppSettings
类的定义吗?是的,先给我们看看AppSettings类:)即使你得到了抖动,你还能运行这个应用吗?您是否尝试过重建、关闭和打开解决方案/VS?问题在于,在同一UI(VisualTree)的两个部分中重用了UIElement。我只是不知道使用AppSettings类会发生什么。Plz发布其代码。完成-添加了AppSettings类。这是。。。奇怪的你能发布堆栈跟踪,也许还有
AppSettings
类的定义吗?是的,先给我们看看AppSettings类:)即使你得到了抖动,你还能运行这个应用吗?您是否尝试过重建、关闭和打开解决方案/VS?问题在于,在同一UI(VisualTree)的两个部分中重用了UIElement。我只是不知道使用AppSettings类会发生什么。Plz发布其代码。完成-添加了AppSettings类。这是问题的一部分。在