重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務器等服務
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務器等服務
這篇文章主要介紹Xamarin XAML語言中如何使用ContentView視圖作為自定義視圖的父類,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
我們提供的服務有:網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、鐵門關(guān)ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的鐵門關(guān)網(wǎng)站制作公司
自定義視圖的父類:ContentView視圖可以作為自定義視圖的父類。
【示例14-2】以下將自定義一個顏色視圖。具體的操作步驟如下:
(1)創(chuàng)建一個Forms Xaml View文件,命名為ColorView。
(2)打開ColorView.xaml文件,編寫代碼,構(gòu)建自定義顏色視圖。代碼如下:
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ContentViewCustomControls.ColorView">
WidthRequest="70"
HeightRequest="70" />
FontSize="Large"
VerticalOptions="CenterAndExpand" />
VerticalOptions="CenterAndExpand" />
(3)打開ColorView.xaml.cs文件,編寫代碼,實現(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)打開MainPage.xaml文件,編寫代碼,通過顏色視圖實現(xiàn)對內(nèi)容頁面的布局。代碼如下:
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ContentViewCustomControls"
x:Class="ContentViewCustomControls.MainPage">
iOS="0, 20, 0, 0" />
此時運行程序,會看到如圖14.10~14.11所示的效果。
(5)構(gòu)建更復雜的布局模式:在ContentView中可以包含視圖,還可以包括布局,從而構(gòu)建更為復雜的布局模式。
以上是“Xamarin XAML語言中如何使用ContentView視圖作為自定義視圖的父類”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!