Reputation: 391
I am not sure what I am doing wrong here.
I have a CarouselPage and a ContentPage I would like to add the content page to the CarouselPage programmatically but when I do this nothing happens in the app.
I can see the items being added to the list but the pages are not shown when I run code below.
Any help on what I am doing wrong is much appreciated :)
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoApp2.MainPage"
/>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace DemoApp2
{
public partial class MainPage : CarouselPage
{
public MainPage()
{
InitializeComponent();
ObservableCollection<ContentPage> pages = new ObservableCollection<ContentPage>();
pages.Add(new StoryPage("This is page 1"));
pages.Add(new StoryPage("This is page 2"));
pages.Add(new StoryPage("This is page 3"));
pages.Add(new StoryPage("This is page 4"));
pages.Add(new StoryPage("This is page 5"));
this.ItemsSource = pages;
}
}
}
StoryPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoApp2.StoryPage">
<ContentPage.Content>
<StackLayout>
<Label x:Name="YourLableName"
Text="Not Set"
TextColor="Black"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
StoryPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace DemoApp2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StoryPage : ContentPage
{
public string PageText = "";
public StoryPage()
{
InitializeComponent();
PageText = "";
}
public StoryPage(string pageTextIn)
{
InitializeComponent();
YourLableName.TextColor = Color.Black;
YourLableName.Text = pageTextIn;
}
protected override void OnAppearing()
{
Debug.WriteLine("OnAppearing");
}
protected override void OnDisappearing()
{
Debug.WriteLine("OnDisappearing");
}
}
}
Upvotes: 0
Views: 112
Reputation: 89082
the docs have a clear example of doing this. If you want to use an ItemsSource
, you have to supply a DataTemplate
. Otherwise you modify the page's Children
Children.Add(new StoryPage("This is page 1"));
Children.Add(new StoryPage("This is page 2"));
Children.Add(new StoryPage("This is page 3"));
Children.Add(new StoryPage("This is page 4"));
Children.Add(new StoryPage("This is page 5"));
Upvotes: 1