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

问题描述 投票:0回答:1

我收到此错误:

错误1名称空间“ using:MyApps”中不存在名称“ TemplateSelector”

但是我不知道为什么,因为当我创建一个新项目并将相同的代码粘贴到他时,一切都正常了,所以问题仅在我的旧项目中。我也尝试清理或重建项目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;
    }
}
c# windows-8 namespaces windows-store-apps using-directives
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.