名称空间中不存在名称TemplateSelector

时间:2013-10-04 07:16:57

标签: c# windows-8 namespaces windows-store-apps using-directives

嗨,我收到此错误Error 1 The name "TemplateSelector" does not exist in the namespace "using:MyApps",但我不知道为什么,因为当我创建新项目并将相同的代码粘贴到他时,一切正常,所以问题只出现在我的旧项目中。我也尝试清理或重建项目100次并手动删除bin文件夹,但仍无法正常工作。

<Page
x:Class="MyApps.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApps"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

...

 <UserControl>
            <UserControl.Resources>
                <!-- Template for INCOMNIG messages -->
                <DataTemplate x:Key="IncomnigTemplate">
                    <Grid>

                        <Grid  Margin="27,0,0,0"  HorizontalAlignment="Left" Background="#BFE8FF" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"></RowDefinition>
                                <RowDefinition Height="auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding MessengerMessage}" HorizontalAlignment="Left"  Margin="5,5,20,0" VerticalAlignment="Top" Foreground="black"></TextBlock>
                            <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding MessengerTime}" HorizontalAlignment="Left"  Margin="5,0,0,5" VerticalAlignment="Bottom" FontSize="9" Foreground="#908C8C"></TextBlock>
                        </Grid>
                    </Grid>
                </DataTemplate>

                <!-- Template for OUTGOING messages -->
                <DataTemplate x:Key="OutgoinTemplate">
                    <Grid>

                        <Grid  Margin="27,0,0,0"  HorizontalAlignment="Right" Background="Gray" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"></RowDefinition>
                                <RowDefinition Height="auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding MessengerMessage}" HorizontalAlignment="Left"  Margin="5,5,20,0" VerticalAlignment="Top" Foreground="black"></TextBlock>
                            <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding MessengerTime}" HorizontalAlignment="Left"  Margin="5,0,0,5" VerticalAlignment="Bottom" FontSize="9" Foreground="#908C8C"></TextBlock>
                        </Grid>
                    </Grid>
                </DataTemplate>


                <!-- datatemplate selector -->
                <local:TemplateSelector x:Key="MessageTemplateSelector"
                                  EmptyTemplate="{x:Null}"
                                  IncomingMessageTemplate="{StaticResource IncomnigTemplate}"
                                  OutgoingMessageCaptureTemplate="{StaticResource OutgoinTemplate}"/>


            </UserControl.Resources>
            <ListBox ItemTemplateSelector="{StaticResource MessageTemplateSelector}" x:Name="lbChoosenMessagesUsers"  Grid.Column="3" FontSize="13" ItemsSource="{Binding MyDatasCurentUser}" Margin="0,0,50,0">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="IsEnabled" Value="False"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </UserControl>...

项目内的TeplateSelector类:

public class TemplateSelector : DataTemplateSelector
{
    public DataTemplate IncomingMessageTemplate { get; set; }
    public DataTemplate OutgoingMessageCaptureTemplate { get; set; }
    public DataTemplate EmptyTemplate { get; set; }

    public new DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var x = item as Message;
        if (x != null)
        {
            return null;
        }

        return EmptyTemplate;
    }
}

1 个答案:

答案 0 :(得分:1)

我经常遇到这样的问题。我的解决方案很简单:只需关闭所有打开的xaml文件,然后再次构建项目。它对我有用。