Вы можете создать «CardView», используя макет «Рамка», доступный в Xamarin.Forms.
Код выглядит следующим образом:
CardViewTemplate.xml:
<?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="App68.CardViewTemplate">
<ContentView.Content>
<Frame IsClippedToBounds="True"
HasShadow="True"
Margin="30"
BorderColor="White"
BackgroundColor="White" >
<Frame.OutlineColor>
<OnPlatform x:TypeArguments="Color"
Android="Gray"
iOS="Gray"/>
</Frame.OutlineColor>
<Frame.Margin>
<OnPlatform x:TypeArguments="Thickness"
Android="10"
iOS="10"/>
</Frame.Margin>
<StackLayout Orientation="Horizontal">
<Grid VerticalOptions="CenterAndExpand"
Padding="0"
HorizontalOptions="FillAndExpand"
BackgroundColor="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Source="{Binding PreviewImage}" />
<Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding RestaurantName}" />
<Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Text="{Binding Address}" />
<Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Text="{Binding FoodTypes}" />
<Label Grid.Row="3" Grid.Column="1" Text="{Binding Rating}" />
<Label Grid.Row="3" Grid.Column="2" Text="{Binding Time}" />
<Label Grid.Row="3" Grid.Column="3" Text="{Binding Distance}" />
</Grid>
</StackLayout>
</Frame>
</ContentView.Content>
</ContentView>
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:App68"
x:Class="App68.MainPage">
<StackLayout Orientation="Vertical">
<Label Text="CardView Demo in Xamarin Forms"
FontAttributes="Bold"
FontSize="Medium"
VerticalOptions="Start"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
BackgroundColor="Transparent"
HorizontalOptions="CenterAndExpand" />
<ListView x:Name="listView"
SelectedItem="{Binding SelcetedItem,Mode=TwoWay}"
HasUnevenRows="True"
ItemsSource="{Binding lstRestaurants}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:CardViewTemplate/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
Restaurant.cs
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace App68
{
class Restaurant
{
public String PreviewImage { get; set; }
public string RestaurantName { get; set; }
public bool IsFavorite { get; set; }
public string Address { get; set; }
public string FoodTypes { get; set; }
public Image RatingIcon { get; set; }
public double Rating { get; set; }
public Image TimeIcon { get; set; }
public double Time { get; set; }
public Image DistanceIcon { get; set; }
public double Distance { get; set; }
}
}
RestaurantViewModel.cs
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace App68
{
class RestaurantViewModel
{
public IList<Restaurant> lstRestaurants { get; set; }
public object SelectedItem { get; set; }
public RestaurantViewModel()
{
lstRestaurants = new List<Restaurant>();
GenerateCardModel();
}
private void GenerateCardModel()
{
var restaurant1 = new Restaurant()
{
PreviewImage = "Restaurant_1.jpg",
RestaurantName = "Premera restaurant",
IsFavorite = true,
Address = "Avenue Road 256",
FoodTypes = "India Italy Chinese kitchen",
Rating = 4.3,
Time = 150,
Distance = 3
};
lstRestaurants.Add(restaurant1);
var restaurant2 = new Restaurant()
{
PreviewImage = "Restaurant_2.jpg",
RestaurantName = "Premera restaurant",
IsFavorite = true,
Address = "Avenue Road 256",
FoodTypes = "India Italy Chinese kitchen",
Rating = 4.3,
Time = 150,
Distance = 3
};
lstRestaurants.Add(restaurant2);
}
}
}
Эффект заключается в следующем: