Xamarin XAML语言教程ContentView视图作为自定义视图的父类

自定义视图的父类:ContentView视图可以作为自定义视图的父类。

【示例14-2】以下将自定义一个颜色视图。具体的操作步骤如下:

(1)创建一个Forms Xaml View文件,命名为ColorView。

(2)打开ColorView.xaml文件,编写代码,构建自定义颜色视图。代码如下:

  • <?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)打开ColorView.xaml.cs文件,编写代码,实现一些与颜色视图相关的属性。代码如下:

  • 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文件,编写代码,通过颜色视图实现对内容页面的布局。代码如下:

  • <?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>

此时运行程序,会看到如图14.10~14.11所示的效果。

(5)构建更复杂的布局模式:在ContentView中可以包含视图,还可以包括布局,从而构建更为复杂的布局模式。

Xamarin XAML语言教程ContentView视图作为自定义视图的父类的更多相关文章

  1. Xamarin XAML语言教程基本视图ContentView

    Xamarin XAML语言教程基本视图ContentView 基本视图ContentView 视图是用来呈现具体内容,根据呈现内容不同,使用的视图也不同.其中,最常用的视图为ContentView视 ...

  2. Xamarin XAML语言教程基本视图ContentViewg构架范围框架

    Xamarin XAML语言教程基本视图ContentViewg构架范围框架 ContentView视图基本上有三个作用,下面依次介绍. (1)范围框架:ContentView视图可以构建一个范围框架 ...

  3. Xamarin XAML语言教程模板视图TemplatedView(二)

    Xamarin XAML语言教程模板视图TemplatedView(二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于中TemplatedView.代码如下: <? ...

  4. Xamarin XAML语言教程模板视图TemplatedView(一)

    Xamarin XAML语言教程模板视图TemplatedView(一) 模板视图TemplatedView 与模板页面相对的是TemplatedView,它被称为模板视图,它的功能和模板页面类似,也 ...

  5. Xamarin XAML语言教程构建ControlTemplate控件模板

    Xamarin XAML语言教程构建ControlTemplate控件模板 控件模板ControlTemplate ControlTemplate是从Xamarin.Forms 2.1.0开始被引入的 ...

  6. Xamarin XAML语言教程基本页面ContentPage占用面积

    Xamarin XAML语言教程基本页面ContentPage占用面积 基本页面和基本视图都是在开发应用程序时最为常用的.本章将讲解有关基本页面ContentPag.基本视图ContentView.控 ...

  7. Xamarin XAML语言教程基础语法篇大学霸

    Xamarin XAML语言教程基础语法篇大学霸 前  言 Xamarin是一个跨平台开发框架.它可以用来开发iOS.Android.Windows Phone和Mac的应用程序.使用Xamarin框 ...

  8. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...

  9. Xamarin XAML语言教程构建ControlTemplate控件模板 (三)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (三) (3)打开MainPage.xaml.cs文件,编写代码,实现主题的切换功能.代码如下: using System; ...

随机推荐

  1. BZOJ1093 [ZJOI2007]最大半连通子图 【tarjan缩点 + DAG最长路计数】

    题目 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G ...

  2. C&C++——C与C++知识点

    C++知识点系列之一(转+整理) 编程时类声明后面千万不要忘了加分号,不然会出现很多错误!! c系列之一一.#include “filename.h”和#include<filename.h&g ...

  3. Optimize Prime Sieve

    /// A heavily optimized sieve #include <cstdio> #include <cstring> #include <algorith ...

  4. bzoj Gty的超级妹子树 块状树

    Gty的超级妹子树 Time Limit: 7 Sec  Memory Limit: 32 MBSubmit: 500  Solved: 122[Submit][Status][Discuss] De ...

  5. es6+最佳入门实践(14)

    14.模版字符串 模版字符串(template string)是增强版的字符串,定义一个模版字符串需要用到反引号 let s = `这是一个模版字符串` console.log(s) 14.1.模版字 ...

  6. noip2016 普及组

    T1 买铅笔 题目传送门 #include<cstdio> #include<cstring> #include<algorithm> using namespac ...

  7. Codeforces Round #299 Div2 解题报告

    这场比赛并没有打现场,昨天晚上做了ABCD四道题,今天做掉了E题 以前还没有过切完一场比赛的所有题呢~爽~ A. Tavas and Nafas   Today Tavas got his test ...

  8. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

  9. 命令行工具PathMarker

    一直使用Guake 终端,Guake提供的其中一个功能是快速打开. 大概的意思就是,显示在终端上的数据会经过匹配,如果符合一定的规则,则可以按住ctrl,使用鼠标单击以触发指定操作. 比如对于一个文件 ...

  10. 【转】使用者角度看bionic pthread_mutex和linux futex实现

    使用者角度看bionic pthread_mutex和linux futex实现 本文所大篇幅引用的参考文章主要描述针对glibc和pthread实现:而本文的考察代码主要是android的bioni ...