【问题标题】:Key attribute can be used only on a tag contained in an IDictionary type property?键属性只能用于 IDictionary 类型属性中包含的标记?
【发布时间】:2011-11-11 03:00:38
【问题描述】:

我不知道这意味着什么,但是在我的 Windows Phone 应用程序 (C#) 中使用这一位 XAML,它给了我错误“键属性只能用于 IDictionary 类型属性中包含的标记。”

<local:Settings x:Key="appSettings"></local:Settings>

有什么想法吗?

编辑:以此为参考:http://msdn.microsoft.com/en-us/library/ff769510(v=vs.92).aspx

根据要求:

<phone:PhoneApplicationPage
    x:Class="Google_.Settings"
    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:Google_"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <!--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 x:Name="TitlePanel" Margin="12,20,0,25">
            <TextBlock x:Name="ApplicationTitle" Text="GOOGLE+" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <CheckBox Content="Warning on exit" Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="12,0,0,0" Name="exitAlert" VerticalAlignment="Top" Width="456" IsChecked="{Binding Source={StaticResource appSettings}, Path=CheckBoxSetting, Mode=TwoWay}"/>
        <!--<CheckBox Content="Enable location data" Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="12,72,0,0" Name="location" VerticalAlignment="Top" Width="456" IsChecked="True" />
        <CheckBox Content="Lock orienation to portrait" Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="12,144,0,0" Name="orientation" VerticalAlignment="Top" Width="456" />-->

        <local:Settings x:Key="appSettings"></local:Settings>

    </Grid>

</phone:PhoneApplicationPage>

【问题讨论】:

    标签: c# xml xaml windows-phone-7


    【解决方案1】:

    x:Key 指定字典中项目的键。

    您只能在要进入字典的标签中使用它,例如在&lt;Resources&gt; 集合中。

    【讨论】:

    • 那么我该如何解决...?它说我应该像这样包装它:&lt;phone:PhoneApplicationPage.Resources&gt; &lt;local:AppSettings x:Key="appSettings"&gt;&lt;/local:AppSettings&gt; &lt;/phone:PhoneApplicationPage.Resources&gt; 但这会导致错误,因为已经有一个 phone:PhoneApplicaitonPage 部分,如果我将 .Resources 添加到存在的部分,它也会导致错误。
    • 您需要将其添加到现有的phone:PhoneApplicationPage.Resources&gt;,就像任何其他资源一样。 ResourcesPhoneApplicationPage 的属性,它只能设置一次(与 XAML 中的任何其他属性一样)。
    • 你能在这里摘录你的整个 xaml 吗?
    • 发布了整个 XAML。我无法将.Resources 添加到现有的phone:PhoneApplicationPage 而不会导致错误。
    • “解析规则 Element ::= 中出现意外的属性。EmptyElement | (StartElement ElementBody)..”和“System.NullReferenceException 在“/Settings.xaml”上引发:对象引用未设置为实例对象。“/Settings.xaml”
    【解决方案2】:
    <!--Try this -->
    <phone:PhoneApplicationPage
        x:Class="Google_.Settings"
        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:Google_"
    
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
        shell:SystemTray.IsVisible="True">
    
        <phone:PhoneApplicationPage.Resources>
            <local:Settings x:Key="appSettings"></local:Settings>
        </phone:PhoneApplicationPage.Resources>
    
        <!--Your code -->
    
    </phone:PhoneApplicationPage>
    

    【讨论】:

    • 您好,欢迎来到 SO。我正在查看新用户发布的“第一个答案”。留下答案时还请添加一些解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多