In this case / dilemma you can use the Xamarin ContentView class.
You can think of ContentView as a subclass or child of ContentPage. So basically, if you have a Xamarin page, which is a ContentPage. And a ContentView can be a view or a subitem in a ContentPage. Both of them can have their own structure:
A ContentPage XAML (e.g. MainContentPage.xaml)
<?xml version="1.0" encoding="utf-8" ?> <ContentPage> <ContentPage.Content> <StackLayout x:Name="MainLayout"> ... </StackLayout> </ContentPage.Content> </ContentPage>
ContentView XAML (e.g. ChildContentView.xaml)
<?xml version="1.0" encoding="utf-8" ?> <ContentView> <ContentView.Content> <StackLayout> ... </StackLayout> </ContentView.Content> </ContentView>
And the ContentPage class (e.g. MainContentPage.cs) looks something like this:
namespace Your.Package { public class MainContentPage : ContentPage { public MainContentPage() { //Some code here, binding, initialization etc. //Create an object of the ChildContentView code behind/class ChildContentView childView = new ChildContentView(); //Add the childView to the MainLayout of the ContentPage this.MainLayout.Children.Add(childView); InitializeComponent(); } } }
And in your ChildContentView.cs (the code behind), you can basically do the same coding as what you can do in the code for any ContentPage class. You can add a StackLayout, you can make a binding, and you can use other inherited methods from the parent TemplatedView class. For more information see this link .
Adimaano
source share