发布:2023/12/7 15:40:56作者:大数据 来源:大数据 浏览次数:436
将ContentView
类继承于Xamarin.Forms.TemplatedView
CardView
XAML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<ContentView ... x:Name="this" x:Class="CardViewDemo.Controls.CardView"> <Frame BindingContext="{x:Reference this}" BackgroundColor="{Binding CardColor}" BorderColor="{Binding BorderColor}" ...> <Grid> ... <Frame BorderColor="{Binding BorderColor, FallbackValue='Black'}" BackgroundColor="{Binding IconBackgroundColor, FallbackValue='Grey'}" ...> <Image Source="{Binding IconImageSource}" .. /> </Frame> <Label Text="{Binding CardTitle, FallbackValue='Card Title'}" ... /> <BoxView BackgroundColor="{Binding BorderColor, FallbackValue='Black'}" ... /> <Label Text="{Binding CardDescription, FallbackValue='Card description text.'}" ... /> </Grid> </Frame> </ContentView> |
ContentView XAML引用自定义控件必须添加命名空间
1 2 |
<ContentPage ... xmlns:controls="clr-namespace:CardViewDemo.Controls" > |
添加引用后即可将控件加入代码XAML
1 2 3 4 5 |
<controls:CardView BorderColor="DarkGray" CardTitle="Slavko Vlasic" CardDescription="Lorem ipsum dolor sit..." IconBackgroundColor="SlateGray" IconImageSource="user.png"/> |
重写样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<?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:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:CardViewDemo.Controls" mc:Ignorable="d" x:Class="CardViewDemo.ControlTemplatePage" Title="ControlTemplate XAML Demo" Padding="10"> <ContentPage.Resources> <ResourceDictionary> <ControlTemplate x:Key="CardViewCompressed"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="100" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Source="{TemplateBinding IconImageSource}" BackgroundColor="{TemplateBinding IconBackgroundColor}" WidthRequest="100" HeightRequest="100" Aspect="AspectFill" HorizontalOptions="Center" VerticalOptions="Center" /> <StackLayout Grid.Column="1"> <Label Text="{TemplateBinding CardTitle}" FontAttributes="Bold" /> <Label Text="{TemplateBinding CardDescription}" /> </StackLayout> </Grid> </ControlTemplate> </ResourceDictionary> </ContentPage.Resources> <StackLayout> <Label Text="A standard CardView control is suitable for grid layouts:" FontAttributes="Italic" TextColor="Red" Margin="0,0,0,10" /> <controls:CardView BorderColor="DarkGray" CardTitle="Slavko Vlasic" CardDescription="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elit dolor, convallis non interdum." IconBackgroundColor="SlateGray" IconImageSource="user.png" /> <Label Text="A ControlTemplate overrides the standard view, creating a more compact view suitable for lists:" FontAttributes="Italic" TextColor="Red" Margin="0,0,0,10" /> <controls:CardView BorderColor="DarkGray" CardTitle="Carolina Pena" CardDescription="Phasellus eu convallis mi. In tempus augue eu dignissim fermentum. Morbi ut lacus vitae eros lacinia." IconBackgroundColor="SlateGray" IconImageSource="user.png" ControlTemplate="{StaticResource CardViewCompressed}" /> <controls:CardView BorderColor="DarkGray" CardTitle="Wade Blanks" CardDescription="Aliquam sagittis, odio lacinia fermentum dictum, mi erat scelerisque erat, quis aliquet arcu." IconBackgroundColor="SlateGray" IconImageSource="user.png" ControlTemplate="{StaticResource CardViewCompressed}" /> <controls:CardView BorderColor="DarkGray" CardTitle="Colette Quint" CardDescription="In pellentesque odio eget augue elementum lobortis. Sed augue massa, rhoncus eu nisi vitae, egestas." IconBackgroundColor="SlateGray" IconImageSource="user.png" ControlTemplate="{StaticResource CardViewCompressed}" /> </StackLayout> </ContentPage> |
Frame类继承于ContentView
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<Frame BorderColor="Gray" CornerRadius="5" Padding="8"> <StackLayout> <Label Text="Card Example" FontSize="Medium" FontAttributes="Bold" /> <BoxView Color="Gray" HeightRequest="2" HorizontalOptions="Fill" /> <Label Text="Frames can wrap more complex layouts to create more complex UI components, such as this card!"/> </StackLayout> </Frame> |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4