locked
DataTemplateSelector not working!

    Question

  • Hi everyone.

    Project link: https://www.dropbox.com/s/1ny4th41ks38pa0/view2.7z?dl=0

    I am trying to use different item templates based on the author of the Rss Feed item. I have implemented the following code into MainPage.xaml.cs :

        //Listim dinamik i artikujve
    
        public abstract class DataTemplateSelector : ContentControl
        {
            public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                return null;
            }
    
            protected override void OnContentChanged(object oldContent, object newContent)
            {
                base.OnContentChanged(oldContent, newContent);
    
                ContentTemplate = SelectTemplate(newContent, this);
            }
        }
    
        //endof Abstract Class
    
        public class naqivenenen : DataTemplateSelector
        {
            public DataTemplate BigCardsItem
            {
                get;
                set;
            }
    
            public DataTemplate SmallCardsItem
            {
                get;
                set;
            }
    
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                RssSchema autori = item as RssSchema;
                if (autori.Author != null)
                {
                    if (autori.Author == "JohnDoe")
                    {
                        return BigCardsItem;
                    }
                    else
                    {
                        return SmallCardsItem;
                    }
                }
    
                return base.SelectTemplate(item, container);
            }
        }

    And the following one into MyAppView.xaml

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="using:AppStudio.Controls"
        xmlns:local="using:AppStudio.Views"
        xmlns:html2xaml="using:AppStudio.Controls.Html2Xaml"
        xmlns:commands="using:AppStudio.Commands">
    
        
        <DataTemplate x:Key="SelectingTemplate">
            <local:naqivenenen Content="{Binding}"
                                        BigCardsItem="{StaticResource LajmetItem}"
                                        SmallCardsItem="{StaticResource RssItem}"
                                        HorizontalContentAlignment="Stretch" />
        </DataTemplate>
    
    
        <!-- ListCards -->
        <DataTemplate x:Key="RssList">
            <Grid>
                <ListView ItemsSource="{Binding Items}"  SelectionMode="None" IsSwipeEnabled="False" IsItemClickEnabled="True" 
                          commands:ItemClickCommand.Command="{Binding ItemClickCommand}"
                          ScrollViewer.VerticalScrollBarVisibility="Hidden"
                         ItemTemplate="{StaticResource SelectingTemplate}">
                </ListView>
                <ProgressBar Width="400" Height="40" VerticalAlignment="Top" IsIndeterminate="True" Visibility="{Binding ProgressBarVisibility}" />
            </Grid>
        </DataTemplate>
    
        
        
        <!-- ListBigVerticalCards Item -->
        <DataTemplate x:Key="LajmetItem">
            <Grid Style="{StaticResource BoxGrid}" Margin="0,0,0,10" Height="380">
                <Rectangle Width="900"/>
                <Grid Style="{StaticResource BoxGrid}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="280"/>
                        <RowDefinition Height="220"/>
                    </Grid.RowDefinitions>
                    <Image Grid.Row="0" Source="{Binding ImageUrl}" Stretch="UniformToFill" Margin="0,4,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    <Grid Grid.Row="1" Height="220" Margin="10,10,10,10">
                        <Grid Height="210">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Style="{StaticResource BoxTitleStyle}" Text="{Binding Author}" MaxLines="2"/>
                        </Grid>
                    </Grid>
                </Grid>
            </Grid>
        </DataTemplate>
    
    
    
        <!-- ListHorizontalCards Item -->
        <DataTemplate x:Key="RssItem">
            <Grid Height="120" Margin="0,0,0,10" Style="{StaticResource BoxGrid}">                        
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="{Binding ImageUrl}" Stretch="UniformToFill" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                <Grid Grid.Column="1">
                    <Rectangle Width="900" Height="0"/>
                    <Grid Margin="16,5,16,5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Style="{StaticResource BoxTitleStyle}" Text="{Binding Title}" MaxLines="2"/>
                        <TextBlock Grid.Row="1" Margin="0,5,0,0" Style="{StaticResource BoxSubtitleStyle}" Text="{Binding Summary}"/>
                    </Grid>
                </Grid>            
            </Grid>
    
        </DataTemplate>
    
    
    
        <DataTemplate x:Key="Rss1DetailDetail">
            <ScrollViewer ZoomMode="Disabled">
                <StackPanel Margin="12,0">
                    <TextBlock Margin="0,16" Text="{Binding Title}" Style="{StaticResource PageTitleStyle}"/>
                    <Button Margin="0,16,0,0" Style="{StaticResource ImageButtonStyle}" 
                            Command="{Binding Source={StaticResource ActionCommands}, Path=ShowImage}" CommandParameter="{Binding ImageUrl}">
                        <Viewbox MaxWidth="400" MaxHeight="300">
                            <Image Source="{Binding ImageUrl}"/>
                        </Viewbox>
                    </Button>
                    <RichTextBlock Margin="0,12" html2xaml:Properties.Html="{Binding Content}" html2xaml:Properties.ContainsCrlf="False"
                                   Style="{Binding DataContext.DisplayFontSize, ElementName=Flip, Converter={StaticResource RichDescriptionStyleConverter}}"/>
                </StackPanel>
            </ScrollViewer>
        </DataTemplate>
    </ResourceDictionary>


    And the following at MainPage.xaml
                <Grid x:Name="MainFragment" Background="#ffffff">
                <Hub x:Name="Container" Grid.Row="1" Margin="0,32,0,0" Background="{StaticResource AppBackground}" DataContext="{Binding}" HeaderTemplate="{StaticResource PageHeader}" SectionsInViewChanged="OnSectionsInViewChanged">
                <HubSection x:Name="RssSection" Width="360" Header="Rss" HeaderTemplate="{StaticResource HubSectionHeaderTemplate}" DataContext="{Binding MainViewModel.RssModel}"
                            d:DataContext="{d:DesignData Source=/Assets/Data/RssDataSource.json, Type=vm:RssViewModel, IsDesignTimeCreatable=true}"
                            ContentTemplate="{StaticResource SelectingTemplate}" IsHeaderInteractive="{Binding HasMoreItems}" />
                </Hub>
                </Grid>

    When trying to deploy the app, the execution hangs in the splash screen, and this line automatically gets a pointer in the App.g.i.cs file :

    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
                UnhandledException += (sender, e) =>
                {
                    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
                };
    #endif

    If I press continoue the app force-closes. What am I missing here? Any help would be much appreciated !

    • Edited by as__ Thursday, May 14, 2015 4:49 PM
    Tuesday, May 12, 2015 11:42 PM

Answers

All replies

  • Hello,

    >>When trying to deploy the app, the execution hangs in the splash screen, and this line automatically gets a pointer in the App.g.i.cs file :

    When you debug the app using visual studio, can you see that error? There should be more information in e object in UnhandledException event handler. Please submit a repro project here to help troubleshooting. We cannot provide useful information if the problem is not clear.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 13, 2015 6:43 AM
    Moderator
  • Here is a link to the full project http://1drv.ms/1AWWerr
    Wednesday, May 13, 2015 3:38 PM
  • Please check out the shared link, I cannot see available project.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 14, 2015 5:38 AM
    Moderator
  • Sorry, for reasons unknown I can not seem to share the compressed project using OneDrive, so I had to use DropBox. Here is the link to the full project https://www.dropbox.com/s/1ny4th41ks38pa0/view2.7z?dl=0
    Thursday, May 14, 2015 4:48 PM
  • Help...Please???
    Monday, May 18, 2015 11:11 PM
  • Hello,

    Sorry for late. I got this exception “the type “%0” was not found.” when I have debugged the windows phone version app. I solved that problem by changing property of RssViews.xaml build action to “Page”. That file in "{phone project}\Views\DataTemplates\RssViews.xaml". You need refer to the following link to find it in detail. https://social.msdn.microsoft.com/Forums/windowsapps/en-US/e1ce3734-b538-4ce2-8e81-c86253e572af/cannot-declare-the-resource-key-for-the-converter-in-genericxaml?forum=winappswithcsharp.

    I also tested the windows version, it worked well.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 3:01 AM
    Moderator
  • Thanks a lot man, you the best !
    Saturday, May 23, 2015 10:16 PM