ContentPage:
The content page is the simplest page without any particular features. It is the blank template to start the blank project.
Content Page displays a single view, often a container such as stack layout or scroll view. We can add any control inside the content page.
Using ContentPage:
We can add content page in our project using two ways.
Now our New project has been created with content page template. Open MainPage.xaml file from solution explorer, and there you find the content page template.
Declaration:
Or we can use the following XAML or C# code to Use content page in Xamarin.Forms project.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:pages"
x:Class="pages.MainPage">
<!--add views-->
<Label Text="Welcome to InfoBrother!"
VerticalOptions="Center"
HorizontalOptions="Center"
/>
</ContentPage>
using System;
using Xamarin.Forms;
namespace pages
{
public class Page : ContentPage
{
public Page ()
{
//add views:
Content = new Label
{
Text = "Welcome to InfoBrother!",
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
}
}
}
Consider the following examples where we use some views in Xamarin.forms content page.
Using Stack Layout:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:pages"
x:Class="pages.MainPage">
<ContentPage.Content>
<!--Layout Orientation Vertically-->
<StackLayout Padding="10" Spacing="10" Orientation="Vertical">
<Button Text="Stack Layout"/>
<BoxView
Color="#FF008080"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
/>
<BoxView
Color="#FF7FBA00"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
/>
<BoxView
Color="#FF01A4EF"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
/>
<BoxView
Color="#FFFFB901"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using Xamarin.Forms;
namespace pages
{
public class UsingStack : ContentPage
{
public UsingStack()
{
//Creating Layout:
var layout = new StackLayout();
//Creating Views:
var button = new Button
{
Text = "Stack Layout"
};
var tealbox = new BoxView
{
Color = Color.FromHex("FF008080"),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
var greenbox = new BoxView
{
Color = Color.FromHex("FF7FBA00"),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
var bluebox = new BoxView
{
Color = Color.FromHex("FF01A4EF"),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
var orangeobx = new BoxView
{
Color = Color.FromHex("FFFFB901"),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand
};
//Adding views:
layout.Children.Add(button);
layout.Children.Add(tealbox);
layout.Children.Add(greenbox);
layout.Children.Add(bluebox);
layout.Children.Add(orangeobx);
Content = layout;
}
}
}
Using Web View:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:pages"
x:Class="pages.MainPage">
<ContentPage.Content>
<WebView
Source="http://www.infobrother.com/"
/>
</ContentPage.Content>
</ContentPage>
using System;
using Xamarin.Forms;
namespace pages
{
public class UsingStack : ContentPage
{
public UsingStack()
{
var website = new WebView();
website.Source = "http://www.infobrother.com/";
Content = website;
}
}
}
WRITE FOR INFOBROTHER