Xamarin XAML Language Tutorial Contentview view as the parent of a custom view

Source: Internet
Author: User

Xamarin XAML Language Tutorial Contentview view as the parent of a custom view

Parent class for custom views: The Contentview view can be the parent of a custom view.

"Example 14-2" The following will customize a color view. The following steps are described:

(1) Create a Forms Xaml view file named Colorview.

(2) Open the Colorview.xaml file, write code, and build a custom color view. The code is as follows:

  • <?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= "/>"
  • <StackLayout>
  • <label x:name= "Colornamelabel"
  • Fontsize= "Large"
  • verticaloptions= "Centerandexpand"/>
  • <label x:name= "Colorvaluelabel"
  • verticaloptions= "Centerandexpand"/>
  • </StackLayout>
  • </StackLayout>
  • </Frame>
  • </ContentView>

(3) Open the ColorView.xaml.cs file, write the code, and implement some properties related to the color view. The code is as follows:

  • 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 ();
  • }
  • Color name
  • 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) Open the MainPage.xaml file, write code, and implement the layout of the content page through the color view. The code is as follows:

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

When you run the program, you see the effect shown in 14.10~14.11.

(5) Build more complex layout patterns: You can include views in Contentview, and you can include layouts to build more complex layout patterns.

Xamarin XAML Language Tutorial Contentview view as the parent of a custom view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.