Xamarin XAML語言教程基本頁面ContentPage佔用面積內容頁面的派生關係與屬性

大學霸發表於2017-07-07

Xamarin XAML語言教程基本頁面ContentPage佔用面積內容頁面的派生關係與屬性

3.內容頁面的派生關係

ContentPage頁面派生自Page,同時它又是其他頁面的父類。派生關係圖如圖14.6所示。


Xamarin XAML語言教程基本頁面ContentPage佔用面積內容頁面的派生關係與屬性
圖14.6  派生關係


注意:ContentPage頁面可以作為其他頁面的一個元素使用。

4.屬性

ContentPage頁面定義了一個用來設定頁面內容的屬性Content屬性,開發者可以將這個屬性設定為一個控制元件、一個檢視或者是一個佈局。

1)開發者可以將Content屬性的屬性設定為按鈕控制元件、標籤控制元件等,如以下的程式碼片段:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:local="clr-namespace:App2"

             x:Class="App2.MainPage">

  <Label Text="Welcome to Xamarin Forms!"

           VerticalOptions="Center"

           HorizontalOptions="Center" />

</ContentPage>

在此程式碼中我們將Content屬性設定為了標籤控制元件。

注意:在屬性和屬性值一章中,我們提到了內容屬性是可以省略的。Content屬性就是一個內容屬性,所以我們在程式碼中將此屬性進行了省略。

2Content屬性除了可以設定為控制元件外,還可以設定為一個內容檢視,如以下的程式碼:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:local="clr-namespace:App2"

             x:Class="App2.MainPage">

  <ContentView>

    <Label Text="Accept what was and what is, and you’ll have more positive energy to pursue what will be."

           VerticalOptions="Center"

           HorizontalOptions="Center" />

  </ContentView>

</ContentPage>

3ContentPage頁面的Content屬性也可以設定為一個物件。當我們將其設定為控制元件或者是內容檢視時,只會在頁面上看到一個元素。如果開發者要在頁面上出現多個元素,就需要使用到佈局,在佈局中可以有多個檢視或者控制元件。如以下的程式碼:

<?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:App2"

             x:Class="App2.MainPage">

  <StackLayout Spacing="10"

               VerticalOptions="CenterAndExpand"

                 HorizontalOptions="Center">

    <Label Text="靜夜思"

       FontSize="30"

       FontAttributes="Bold"

       HorizontalOptions="Center"/>

    <Label Text="床前明月光,"

           FontSize="18"/>

    <Label Text="疑是地上霜。"

           FontSize="18"/>

    <Label Text="舉頭望明月,"

           FontSize="18"/>

    <Label Text="低頭思故鄉。"

           FontSize="18"/>

  </StackLayout>

</ContentPage>

在此程式碼中,我們將佈局設定為了堆疊佈局,在此佈局中又放置了5個標籤控制元件。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29597077/viewspace-2141691/,如需轉載,請註明出處,否則將追究法律責任。

相關文章