Forum Xamarin.Forms

DataTemplateSelector in .net standard not working ?

I'm currently developping Crossplatform app that i need to show a feed of activities, each activity has its own content type and layout so i have created 5 different templates 1 for each activity type. My binding works in the sense that it calls the good template but i'm unable to show the content that i have gotten from my API...

i get a list of wich is used in my list view source
and i set its template to the datatemplate selector i have created

My page code

public ProfilePage ()
{
InitializeComponent ();
NavigationPage.SetHasNavigationBar(this, false);
ProfilePageInfos infos = API.GetProfilePageInfos();
IMG_ProfileAvatar.Source = infos.GetUserAvatarLink();
ProfilePoints.Text = infos.GetUserPoints().GetTotal() + " points";
ProfileName.Text = infos.GetUserName();

    AddGestureRecognizer(BTNHome, GetHomeGesture());
    AddGestureRecognizer(BTNPortfolio, GetPortfolioGesture());
    AddGestureRecognizer(BTNFriends, GetFriendsGesture());

    List<Feed> feed = API.GetHomeActivityFeed();
    ProfileActivities.ItemsSource = feed;
    ProfileActivities.ItemTemplate = new ActivityDataTemplateSelector();
}

My Feed class:
public class Feed
{
public Activity Activity { get; set; }
public string Name = "test name binding";
public Feed(Activity activity)
{
this.Activity = activity;
}
}

My datatemplate selector
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var feed = item as Feed;
if (feed == null)
return null;
if (feed.Activity.action == "challenge_completed")
return this.activityCompletedChallenge;
else if (feed.Activity.action == "challenge_accepted")
return this.activityAcceptedChallenge;
else if (feed.Activity.action == "achievement_create")
return this.activityCustomAchievement;
else if (feed.Activity.action == "user_leveled")
{
return this.activityLevelUp;
}
else if (feed.Activity.action == "user_rank_up")
return this.activityRankUp;
else return null;
}
and My Template:
<?xml version="1.0" encoding="UTF-8"?> <ViewCell.View> <Grid.RowDefinitions> </Grid.RowDefinitions> <Grid.ColumnDefinitions> </Grid.ColumnDefinitions>

Thanks alot and happy holly days !

Answers

  • ChrissIanChrissIan USMember

    <ViewCell xmlns="" xmlns:x="" x:Class="MyaMobile.Shared_Classes.ViewCells.ActivityLevelUp"> <ViewCell.View> <StackLayout > <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <!-- Activity title --> <Image x:Name="ActorAvatar" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" WidthRequest="50" HeightRequest="50"/> <Label x:Name="ActorName" Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/> <Label x:Name="ActivityAction" Grid.Row="0" Grid.Column="2"/> <Label x:Name="ActivityPosted" Grid.Row="1" Grid.Column="1"/> <!-- Activity Content --> <Label x:Name="ActivityTitle" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" /> <!-- Activity Footer --> </Grid> <Label Text="Level Up" /> </StackLayout> </ViewCell.View> </ViewCell>

Sign In or Register to comment.