【问题标题】:The name TemplateSelector does not exist in the namespace命名空间中不存在名称 TemplateSelector
【发布时间】:2020-03-10 19:42:16
【问题描述】:

我收到此错误:

错误 1 ​​名称“TemplateSelector”在命名空间“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;
    }
}

【问题讨论】:

  • 您能否粘贴UserControl 的XMLNS 声明或它的withing 页面?
  • 是的,它的内页 xaml。它的链接在这里>stackoverflow.com/questions/18757615/…
  • 除此之外还有其他错误吗?
  • UserControl的内容是什么?为什么它是空的?您是否尝试将DataTemplate 放入Page.Resouurce
  • 你在哪里使用用户控件?用户控件应位于单独的文件中。为什么它在代码之间?

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


【解决方案1】:

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

【讨论】:

  • 是的,我尝试清理、重建、关闭所有等,重新启动我的电脑,清理 bin 文件夹等,但问题仍然存在。正如我所说的代码很好,因为当我在新项目中使用它时没问题,它看起来像一些 VS 缓存文件问题或者我不知道。
  • 将这个 public new DataTemplate SelectTemplate(object item, DependencyObject container) 更改为 public override DataTemplate SelectTemplateCore(object item, DependencyObject container)
  • 我对它进行了修改,但在覆盖“受保护”继承成员时出现无法更改访问修饰符,因此我将公共更改为受保护,但我不确定它是否正常,但名称空间的问题仍然存在
  • 是的,应该是保护的。你在构建的时候关闭了所有的xaml文件吗?
  • 是的,一切都已关闭,但我认为当我重建它的干净并构建源代码时,是的,所有页面和类都已关闭。
猜你喜欢
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 2012-06-19
  • 1970-01-01
相关资源
最近更新 更多