您好,登錄后才能下訂單哦!
這篇文章主要介紹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所示的效果。
(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è)資訊頻道!
免責(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)容。