c# - 命名空间中不存在名称 TemplateSelector

标签 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;
    }
}

最佳答案

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

关于c# - 命名空间中不存在名称 TemplateSelector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19175446/

相关文章:

c# - 如何将枚举保存到 ApplicationData.Current.LocalSettings

namespaces - Hyperledger Composer 系统命名空间

c# - 地铁风格应用程序 : How to call a C# method from javascript code running in a webview control

windows-8 - metro (XAML/C#) : detect installation and/or first run

c# - 从 ASP.NET Core Web 应用访问类库中的 Entity Framework

c# - 使用 sqlite-net 删除行的 SQLite 异常

javascript - 这是防止 JavaScript 中命名空间污染的好方法吗?

c++ - 命名空间和 C++

c# - 构建一个安装程序,包括 mysql 连接器 C#

c# - ConnectString 在 C# 中不起作用