溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Xamarin XAML語(yǔ)言中如何使用ContentView視圖作為自定義視圖的父類(lèi)

發(fā)布時(shí)間:2021-12-21 13:47:22 來(lái)源:億速云 閱讀:158 作者:小新 欄目:互聯(lián)網(wǎng)科技

這篇文章主要介紹Xamarin XAML語(yǔ)言中如何使用ContentView視圖作為自定義視圖的父類(lèi),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

自定義視圖的父類(lèi):ContentView視圖可以作為自定義視圖的父類(lèi)。

【示例14-2】以下將自定義一個(gè)顏色視圖。具體的操作步驟如下:

1)創(chuàng)建一個(gè)Forms Xaml View文件,命名為ColorView。

2)打開(kāi)ColorView.xaml文件,編寫(xiě)代碼,構(gòu)建自定義顏色視圖。代碼如下:

  • <?xml version="1.0" encoding="UTF-8"?>

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

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

  •              x:Class="ContentViewCustomControls.ColorView">

  •   <Frame OutlineColor="Accent">

  •     <StackLayout Orientation="Horizontal">

  •       <BoxView x:Name="boxView"

  •                WidthRequest="70"

  •       HeightRequest="70" />

  •       <StackLayout>

  •         <Label x:Name="colorNameLabel"

  •                FontSize="Large"

  •                VerticalOptions="CenterAndExpand" />

  •         <Label x:Name="colorValueLabel"

  •                VerticalOptions="CenterAndExpand" />

  •       </StackLayout>

  •     </StackLayout>

  •   </Frame>

  • </ContentView>

3)打開(kāi)ColorView.xaml.cs文件,編寫(xiě)代碼,實(shí)現(xiàn)一些與顏色視圖相關(guān)的屬性。代碼如下:

  • using System;

  • using System.Collections.Generic;

  • using System.Linq;

  • using System.Text;

  • using System.Threading.Tasks;

  • using Xamarin.Forms;

  • namespace ContentViewCustomControls

  • {

  •     public partial class ColorView : ContentView

  •     {

  •         string colorName;

  •         ColorTypeConverter colorTypeConv = new ColorTypeConverter();

  •         public ColorView()

  •         {

  •             InitializeComponent();

  •         }

  •         //顏色名稱

  •         public string ColorName

  •         {

  •             set

  •             {

  •                 colorName = value;

  •                 colorNameLabel.Text = value;

  •                 Color color = (Color)colorTypeConv.ConvertFromInvariantString(colorName);

  •                 boxView.Color = color;

  •                 colorValueLabel.Text = String.Format("{0:X2}-{1:X2}-{2:X2}",

  •                 (int)(255 * color.R),

  •                 (int)(255 * color.G),

  •                 (int)(255 * color.B));

  •             }

  •             get

  •             {

  •                 return colorName;

  •             }

  •         }

  •     }

  • }

4)打開(kāi)MainPage.xaml文件,編寫(xiě)代碼,通過(guò)顏色視圖實(shí)現(xiàn)對(duì)內(nèi)容頁(yè)面的布局。代碼如下:

  • <?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:ContentViewCustomControls"

  •              x:Class="ContentViewCustomControls.MainPage">

  •   <ContentPage.Padding>

  •     <OnPlatform x:TypeArguments="Thickness"

  •                 iOS="0, 20, 0, 0" />

  •   </ContentPage.Padding>

  •   <StackLayout Padding="6, 0">

  •     <local:ColorView ColorName="Aqua" />

  •     <local:ColorView ColorName="Black" />

  •     <local:ColorView ColorName="Blue" />

  •     <local:ColorView ColorName="Fuchsia" />

  •     <local:ColorView ColorName="Gray" />

  •   </StackLayout>

  • </ContentPage>

此時(shí)運(yùn)行程序,會(huì)看到如圖14.10~14.11所示的效果。

 Xamarin XAML語(yǔ)言中如何使用ContentView視圖作為自定義視圖的父類(lèi)

5)構(gòu)建更復(fù)雜的布局模式:在ContentView中可以包含視圖,還可以包括布局,從而構(gòu)建更為復(fù)雜的布局模式。

以上是“Xamarin XAML語(yǔ)言中如何使用ContentView視圖作為自定義視圖的父類(lèi)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI