找不到具有名称/密钥PickerItemTemplate的资源

时间:2014-08-04 08:25:45

标签: c# xaml windows-phone-8 windows-phone

我正在创建一个Windows Phone应用程序。我面临Xaml Parser例外。以下是我的XAML代码:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Border Background="LightGreen" Width="34" Height="34">
                    <TextBlock Text="{Binding BloodGroupItems}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
               </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                <TextBlock Text="{Binding BloodGroupItems}" Margin="16 0 0 0" FontSize="43" />
                </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

ListPicker:

 <StackPanel Orientation="Horizontal">
     <Grid>
         <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
         </Grid.RowDefinitions>

         <TextBlock x:Name="BoolType" Margin="10,0,0,0" FontSize="30" Grid.Row="0"
                   Text="blood group"></TextBlock>
         <StackPanel Grid.Row="1"  Orientation="Horizontal">
             <toolkit:ListPicker x:Name="listPicker" 
                      ItemTemplate="{StaticResource PickerItemTemplate}" 
                      FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"   
                      FullModeHeader="Cities" SelectedIndex="2"
                      CacheMode="BitmapCache"
                      Header="blood" Width="114"/>
         </StackPanel>
     </Grid>
 </StackPanel>

我的代码选择列表项:

  public ProfilePage()
    {
        InitializeComponent();
        this.listPicker.ItemsSource = GetBloodItems();           
    }

    public class BloodGroupDetail
    {
        public string BloodGroupItems { get; set; }
    }

    private List<BloodGroupDetail> GetBloodItems()
    {
        List<BloodGroupDetail> BloodDetails = new List<BloodGroupDetail>
        {
            new BloodGroupDetail { BloodGroupItems="A+"},
            new BloodGroupDetail { BloodGroupItems="B+"},
            new BloodGroupDetail { BloodGroupItems="AB+"},
            new BloodGroupDetail { BloodGroupItems="O+"}                
        };
        return BloodDetails;
    }

我收到以下异常:

enter image description here

我是Windows Apps的新手。

更新的代码:

 <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Border Background="LightGreen" Width="34" Height="34">
                    <TextBlock Text="{Binding BloodGroupItems}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                <TextBlock Text="{Binding BloodGroupItems}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
                </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="user profile" 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">
        <Grid x:Name="ProfilePanel"  Margin="12,0,12,0">
            <ScrollViewer>
                <StackPanel Margin="0,4,16,0" Orientation="Vertical">
                    <TextBox x:Name="NameTB" Text="name" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <TextBox x:Name="AgeTB" InputScope="Number" Text="age" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray"  IsReadOnly="False" BorderThickness="0"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonsex1" Content="male" GroupName="Group1" Checked="RadioButtonGender_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonsex2" Content="female" GroupName="Group1" Checked="RadioButtonGender_Checked"/>
                    </StackPanel>
                    <TextBox x:Name="PincodeTB" InputScope="Number" Text="pincode" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <TextBox x:Name="ContactTB" InputScope="Number" Text="contact no" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonvisible1" Content="public" GroupName="Group2" Checked="RadioButtonVisible_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonvisible2" Content="private" GroupName="Group2" Checked="RadioButtonVisible_Checked"/>
                    </StackPanel>
                    <toolkit:ListPicker x:Name="listPicker" ItemTemplate="{StaticResource PickerItemTemplate}"  FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"   FullModeHeader="Cities" SelectedIndex="2"  CacheMode="BitmapCache"
                            Header="blood group"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonstatus1" Content="active" GroupName="Group3" Checked="RadioButtonStatus_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonstatus2" Content="inactive" GroupName="Group3" Checked="RadioButtonStatus_Checked"/>
                    </StackPanel>
                    <Button x:Name="btnSubmit" Content="Register" Tap="btnSubmit_Tap"  Width="200"></Button>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>
</Grid>

2 个答案:

答案 0 :(得分:0)

问题是您向模板提供了x:Name而不是x:Key

替换它:

<DataTemplate x:Name="PickerItemTemplate">

使用此代码:

<DataTemplate x:Key="PickerItemTemplate">

DataTemplates由Key访问。如果由于某种原因您还需要Name,您也可以同时使用这两个属性。

答案 1 :(得分:0)

我解决了。

以下是我的XAML页面:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <StackPanel Orientation="Horizontal">
                <Border Background="LightGreen" Width="34" Height="34">
                    <TextBlock Text="{Binding BloodGroupItems}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <TextBlock Text="{Binding BloodGroupItems}" Margin="12 0 0 0"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="16 21 0 20">
                <TextBlock Text="{Binding BloodGroupItems}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    <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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="user profile" 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">
        <Grid x:Name="ProfilePanel"  Margin="12,0,12,0">
            <ScrollViewer>
                <StackPanel Margin="0,4,16,0" Orientation="Vertical">
                    <TextBox x:Name="NameTB" Text="name" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <TextBox x:Name="AgeTB" InputScope="Number" Text="age" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray"  IsReadOnly="False" BorderThickness="0"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonsex1" Content="male" GroupName="Group1" Checked="RadioButtonGender_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonsex2" Content="female" GroupName="Group1" Checked="RadioButtonGender_Checked"/>
                    </StackPanel>
                    <TextBox x:Name="PincodeTB" InputScope="Number" Text="pincode" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <TextBox x:Name="ContactTB" InputScope="Number" Text="contact no" GotFocus="OnFocus" LostFocus="OnLostFocus" HorizontalAlignment="Left" Width="370" FontSize="22" Foreground="Gray" IsReadOnly="False" BorderThickness="0"/>
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonvisible1" Content="public" GroupName="Group2" Checked="RadioButtonVisible_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonvisible2" Content="private" GroupName="Group2" Checked="RadioButtonVisible_Checked"/>
                    </StackPanel>
                    <toolkit:ListPicker x:Name="listPicker" ItemTemplate="{StaticResource PickerItemTemplate}" 
                            FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" FullModeHeader="Cities" SelectedIndex="2"  CacheMode="BitmapCache"
                            Header="blood group" />
                    <StackPanel Orientation="Horizontal">
                        <RadioButton x:Name="radioButtonstatus1" Content="active" GroupName="Group3" Checked="RadioButtonStatus_Checked"  IsChecked="True" Width="150"/>
                        <RadioButton x:Name="radioButtonstatus2" Content="inactive" GroupName="Group3" Checked="RadioButtonStatus_Checked"/>
                    </StackPanel>
                    <Button x:Name="btnSubmit" Content="Register" Tap="btnSubmit_Tap"  Width="200"></Button>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>

我的代码:

public ProfilePage()
    {
        InitializeComponent();
        listPicker.SetValue(Microsoft.Phone.Controls.ListPicker.ItemCountThresholdProperty, 3);
        List<BloodGroupDetail> source = new List<BloodGroupDetail>();
        source.Add(new BloodGroupDetail() { BloodGroupItems = "A+" });
        source.Add(new BloodGroupDetail() { BloodGroupItems = "B+" });
        source.Add(new BloodGroupDetail() { BloodGroupItems = "O+" });
        source.Add(new BloodGroupDetail() { BloodGroupItems = "AB+" });
        this.listPicker.ItemsSource = source;          
    }

    public class BloodGroupDetail
    {
        public string BloodGroupItems { get; set; }
    }