0

I get this error:

Error 1 The name "TemplateSelector" does not exist in the namespace "using:MyApps"

but I don't know why because when I create new project and paste the same code to him everything is working so problem is only in my old project. I also try clean or rebuild project 100 times and manually delete bin folder but still not work.

<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>

Class TeplateSelector inside project:

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;
    }
}
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
pavol.franek
  • 1,396
  • 3
  • 19
  • 42
  • Can you please paste XMLNS declaration of `UserControl` or it's withing page? – Farhan Ghumra Oct 04 '13 at 07:30
  • yes its inside page xaml. Its linked here> http://stackoverflow.com/questions/18757615/listbox-with-custom-datatemplate – pavol.franek Oct 04 '13 at 07:34
  • Is there any other error apart from it? – Farhan Ghumra Oct 04 '13 at 08:41
  • What's the content of `UserControl`? Why it is empty? Have you tried putting `DataTemplate` in `Page.Resouurce`? – Farhan Ghumra Oct 04 '13 at 08:47
  • No there are not another error and I can build my app when I have this error what is interesting but page where I have used this xaml is clear/demaged. Yes I try put it to page resources but I get the same error. – pavol.franek Oct 04 '13 at 09:10
  • here is full xaml: https://www.dropbox.com/s/6un6ab4u4pu3suk/MessagesPage.xaml – pavol.franek Oct 04 '13 at 09:14
  • 1
    Where are you using usercontrol? User controls should be in separate files. Why it's in between the code? – Farhan Ghumra Oct 04 '13 at 09:51
  • how separate its first time when I use it. Now I have user control code inside xaml page where I have my UI items its bad? If yes can you please tell me how it should be ok – pavol.franek Oct 04 '13 at 09:57
  • I am not sure what you want to achieve. You are using TemplateSelector for listbox, listbox is a part of usercontrol but where is that usercontrol being used? – Farhan Ghumra Oct 04 '13 at 10:16
  • ok I get it to work. now I show messages in UI. Error with namespace is still here but its look like visual studio problem now code working. Thx For time. – pavol.franek Oct 04 '13 at 10:51

1 Answers1

2

I've often got problem like this too. My solution is simple: just close all opened XAML files, and then build the project again. It works for me.

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
SimonFisher
  • 380
  • 1
  • 4
  • 13
  • Yes I try clean, rebulid, close all etc reboot my PC, clean bin folders etc but problem is still here. As I say code is good because when I use it in new project no problem its look like some VS cache files problem or I dont know. – pavol.franek Oct 04 '13 at 09:08
  • change this public new DataTemplate SelectTemplate(object item, DependencyObject container) to public override DataTemplate SelectTemplateCore(object item, DependencyObject container) – SimonFisher Oct 04 '13 at 09:18
  • I chanhe it but I get Cannot change access modifier when overriding 'protected' inherited member so I change public to protected but Im not sure its OK but problem with namespace is still here – pavol.franek Oct 04 '13 at 09:32
  • Yes,it should be protected.Have you closed all the xaml files when building? – SimonFisher Oct 04 '13 at 09:38
  • yes all was closed but I think when I do rebuild its clean and build sources but yes all pages and classes was closed. – pavol.franek Oct 04 '13 at 09:54