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