命名空间中不存在名称模板选择器

本文关键字:选择器 不存在 命名空间 | 更新日期: 2023-09-27 18:33:48

我收到此错误:

错误 1 命名空间"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 文件,然后再次生成项目。它对我有用。