RelativeLayout in Xamarin: Can't make a square - xamarin.forms

I've grabbed the sample project from here: https://developer.xamarin.com/samples/xamarin-forms/BoxView/ListViewColors/
I'm trying to make visual changes, in particular I'm trying to get the color squares to be rounded circles. The color squares are BoxViews in the original, and they use WidthRequest and HeighRequest, both set to 50. This succeeds in making them square. But:
BoxViews don't have a CornerRadius property, so I've made them Frames instead.
I don't want to use points, I want to use RelativeLayout values, so I've tried to set both width and height using Factor=0.1 of the ItemTemplate's Frame.
It doesn't work! No matter what I do, the colored frames are just a weird oblong shape--they're not even square anymore.
What am I doing wrong?
<ListView SeparatorVisibility="None"
BackgroundColor="Transparent"
ItemsSource="{x:Static local:NamedColor.All}"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1}">
<ListView.RowHeight>
<OnPlatform x:TypeArguments="x:Int32">
<On Platform="iOS, Android" Value="80" />
<On Platform="UWP" Value="90" />
</OnPlatform>
</ListView.RowHeight>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="5">
<Frame x:Name="itemFrame"
BackgroundColor="#bbffffff"
Padding="8"
CornerRadius="10"
HasShadow="false">
<StackLayout Orientation="Horizontal">
<Frame x:Name="colorFrame"
CornerRadius="20"
OutlineColor="Transparent"
BackgroundColor="{Binding Color}"
HasShadow="false"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToView,
ElementName=itemFrame,
Property=Width,
Factor=0.1}"
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToView,
ElementName=itemFrame,
Property=Width,
Factor=0.1}"/>
<StackLayout>
<Label Text="{Binding FriendlyName}"
FontSize="22"
VerticalOptions="StartAndExpand" />
<Label Text="{Binding RgbDisplay, StringFormat='RGB = {0}'}"
FontSize="16"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</StackLayout>
</Frame>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RelativeLayout>
</ContentPage>

The padding in the outer frame seems to have interfered with the shape of your circle, rendering it into an elliptical figure. Removing the padding works and you can obtain a circular shape from your frame.
The corner radius property for a frame usually doesn't render for UWP application. So, you'll need to make a custom renderer for your frame to obtain the circle. As for your listview you can use the following code:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="5">
<RelativeLayout VerticalOptions="FillAndExpand">
<Frame OutlineColor="Accent" CornerRadius="6" HorizontalOptions="FillAndExpand" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1,Constant=0}" >
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=1,Constant=0}" >
<Frame BackgroundColor="{Binding Color}" CornerRadius="60" RelativeLayout.HeightConstraint ="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.6, Constant=0}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=0.6,Constant=0}"/>
<StackLayout HorizontalOptions="FillAndExpand">
<Label Text="{Binding FriendlyName}" FontSize="22" VerticalOptions="StartAndExpand" />
<Label Text="{Binding RgbDisplay, StringFormat='RGB = {0}'}" FontSize="16" VerticalOptions = "CenterAndExpand" />
</StackLayout>
</StackLayout>
</Frame>
</RelativeLayout>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
For frame, custom renderer for UWP:
public class CustomFrame:FrameRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var frame = e.NewElement;
Windows.UI.Color frameBG = Windows.UI.Color.FromArgb(
(byte)(frame.BackgroundColor.A * 255),
(byte)(frame.BackgroundColor.R * 255),
(byte)(frame.BackgroundColor.G * 255),
(byte)(frame.BackgroundColor.B * 255));
Control.CornerRadius = new Windows.UI.Xaml.CornerRadius(frame.CornerRadius);
Control.Background = new SolidColorBrush(frameBG);
frame.BackgroundColor = Xamarin.Forms.Color.Transparent;
}
}
}

Related

center item in carouselView

I am struggling to get my item centered in the carousel view, as simple as this sounds for same reason the item always starts at starts.
I need to display the items one at the time.
<StackLayout x:Name="innerStack"
HorizontalOptions="CenterAndExpand">
<Label Text="tesyt"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"></Label>
<CarouselView HorizontalScrollBarVisibility="Never" BackgroundColor="Blue"
x:Name="carousel"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
HeightRequest="500"
> <CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" ItemSpacing="0"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemsSource >
<x:Array Type="{x:Type Label}">
<Label Text="tesyt" TextColor="Green" Margin="10,0,0,0"
></Label>
<Label Text="tesyt" TextColor="Brown"></Label>
</x:Array>
</CarouselView.ItemsSource>
</CarouselView>
</StackLayout>
I am struggling to get my item centered in the carousel view。
If you want to set item center in carouselview, you can set HorizontalOptions="CenterAndExpand" and VerticalOptions="CenterAndExpand" for item in carouselview datatemplate.
And I find there are some error for carouselview.itemsource, you can refer to x:Array markup extension firstly, you can also take a look the following code:
<CarouselView
x:Name="carousel"
BackgroundColor="Blue"
HeightRequest="500"
HorizontalOptions="CenterAndExpand"
HorizontalScrollBarVisibility="Never"
VerticalOptions="CenterAndExpand">
<CarouselView.ItemsLayout>
<LinearItemsLayout ItemSpacing="20" Orientation="Horizontal" />
</CarouselView.ItemsLayout>
<CarouselView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>mono</x:String>
<x:String>monodroid</x:String>
<x:String>monotouch</x:String>
<x:String>monorail</x:String>
<x:String>monodevelop</x:String>
<x:String>monotone</x:String>
<x:String>monopoly</x:String>
<x:String>monomodal</x:String>
<x:String>mononucleosis</x:String>
</x:Array>
</CarouselView.ItemsSource>
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame
Margin="20"
BorderColor="Red"
CornerRadius="5"
HasShadow="True"
HeightRequest="50"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<Label
HorizontalOptions="CenterAndExpand"
Text="{Binding .}"
VerticalOptions="CenterAndExpand" />
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>

Top positioned Xamarin forms listview with a header - problem: top position varies on some iPhone models

I need to set the height of the list view so that the bottom view cell is not cut off whenever adding or removing a view item. I can achieve it for some iPhone models, but not the others. It seems it’s not a right way to achieve it by iPhone model. My questions are:
1. On some iPhone models, why the top position shifts down by about 25 pixels after adding a view item?
2. How to find the absolute top position Y coordinate of the list view?
It seems AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" doesn’t help. VerticalOptions="Start" doesn’t help either.
Note:
1. no such an issue on Android phones.
2. Observation only: It seems that changing the margin of the listview header brandStack has no effect on the layout, eg. cannot see increased margins.
3. The following two screenshots show that the top position of the listview header has moved down a bit after an item is added to the listview.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="tecommobile.Views.PanelsHomePage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:tecommobile.Effects;assembly=tecommobile"
xmlns:tecommobile="clr-namespace:tecommobile;assembly=tecommobile"
xmlns:views="clr-namespace:tecommobile.Views;assembly=tecommobile"
xmlns:controls="clr-namespace:Flex.Controls;assembly=Flex"
xmlns:system="clr-namespace:System;assembly=netstandard"
x:Name="PanelHomePage"
Title="{Binding Title}"
BackgroundColor="Bisque"
NavigationPage.HasNavigationBar="false">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="addPanelStyle" TargetType="views:CustomImageButton" BasedOn="{StaticResource customImageButtonStyle}">
<Setter Property="Source" Value="panel_add.png" />
<Setter Property="HeightRequest" Value="70" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<RelativeLayout>
<tecommobile:GradientColorStack RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=1}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1}"
Style="{StaticResource gradientColorStack}">
<StackLayout>
<StackLayout x:Name="panelListViewStack" BackgroundColor="Red"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<ListView x:Name="ItemsListView" BackgroundColor="Orange"
Margin="15"
CachingStrategy="RecycleElement"
IsPullToRefreshEnabled="false"
ItemsSource="{Binding Items}"
RefreshCommand="{Binding LoadItemsCommand}"
SelectionMode="None"
Style="{StaticResource listViewNoSeparatorStyle}"
Scrolled="ItemsListView_OnScrolled">
<ListView.Header>
<StackLayout x:Name="brandStack" Margin="15, 0, 15, 0" HeightRequest="150" BackgroundColor="Yellow">
<Image HeightRequest="48"
HorizontalOptions="Center"
Source="Icon_ILXLinks.png"
WidthRequest="48"
Margin="0,30,0,0" />
<Label FontSize="20"
HorizontalTextAlignment="Center"
Text="TecomPlus"
TextColor="White"
VerticalTextAlignment="Center" />
<Label FontSize="14"
HorizontalTextAlignment="Center"
Text="Your smart security manager"
TextColor="White"
VerticalTextAlignment="Center"
Margin="0,0,0,20" />
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid effects:RoundCornersEffect.CornerRadius="8" HeightRequest="75" Margin="2" BackgroundColor="{Binding PanelColour}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding BindingContext.EditToggleIcon, Source={x:Reference PanelHomePage}}"
BackgroundColor="Transparent" WidthRequest="50" HorizontalOptions="StartAndExpand" Margin="10,0,0,0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.HomePageEditPanelCommand, Source={x:Reference PanelHomePage}}" CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Text}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"
LineBreakMode="NoWrap" FontSize="14" TextColor="{Binding PanelTextColour}">
</Label>
<Image Grid.Row="0" Grid.Column="2" Source="forward.png" Style="{StaticResource rightImageStyle}">
</Image>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.RunPanelCommand, Source={x:Reference PanelHomePage}}" CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<StackLayout HeightRequest="70"></StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BackToEditCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<tecommobile:GradientColorStack.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BackToEditCommand}"/>
</tecommobile:GradientColorStack.GestureRecognizers>
</tecommobile:GradientColorStack>
<views:CustomImageButton Command="{Binding InfoCommand}" BackgroundColor="Transparent"
HeightRequest="30"
WidthRequest="30"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1,
Constant=-45}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0,
Constant=35}"
Source="info.png">
</views:CustomImageButton>
<Grid x:Name="bottomGrid" BackgroundColor="#216593" effects:RoundCornersEffect.CornerRadius="8"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-140}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-70}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.0*" />
<ColumnDefinition Width="1.0*" />
</Grid.ColumnDefinitions>
<Image x:Name="AddButton" Source="panel_add.png" Grid.Row="0" Grid.Column="0" Style="{StaticResource buttonImageStyle}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding AddItemCommand}" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
<Image x:Name="DeleteButton" Source="{Binding BindingContext.DeleteToggleIcon, Source={x:Reference PanelHomePage}}"
Grid.Row="0" Grid.Column="1" Style="{StaticResource buttonImageStyle}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ToggleDeleteIconCommand}" CommandParameter="{x:Reference DeleteButton}" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</Grid>
</RelativeLayout>
</ContentPage.Content>
</ContentPage>
The top position shifts down because the margin you set in the listView:
<ListView BackgroundColor="Orange" Margin="15">
Remove the Margin="15" and the space will disappear.
BTW, you can add page-safe-area-layout to make it looks better:
<ContentPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
Title="Safe Area"
ios:Page.UseSafeArea="true">
<StackLayout>
...
</StackLayout>
</ContentPage>

Floating Button with Numeric Count Value in Xamarin Forms

Unable to add the numeric part to the Floating Button
Just an idea of layouting frame inside RealtiveLayout, do change as per your need.
<RelativeLayout
VerticalOptions="Start"
HorizontalOptions="Start"
Margin="0,30,0,0"
HeightRequest="75"
WidthRequest="75"
IsClippedToBounds="False">
<RelativeLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</RelativeLayout.GestureRecognizers>
<Frame
x:Name="floatMainButton"
Padding="0"
HeightRequest="75"
CornerRadius="38"
WidthRequest="75"/>
<Frame
BackgroundColor="LightBlue"
HeightRequest="25"
WidthRequest="25"
Padding="0"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=floatMainButton, Factor=0.75, Property=Width}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=floatMainButton, Factor=-0.10, Property=Height}"
CornerRadius="13">
<Label
VerticalOptions="Center"
HorizontalOptions="Center"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
TextColor="White"
Text="{Binding Count}"/>
</Frame>
</RelativeLayout>
Is this what you are looking for
Comment for any query...

xamarin forms - image wrapped in frame(with property cornerRadius set) not showing round corners

I am trying to create layout similar to Android cardview.
My progress for now.
image1
As you can see It looks guide well. However when I change Image property Aspect="Fill" to Aspect="AspectFill" images would lost round corners.
image2
My code
<Frame
VerticalOptions="Start"
Margin="8"
CornerRadius="10"
Padding="0"
BackgroundColor="White"
IsClippedToBounds="False">
<StackLayout Spacing="0">
<Image
HeightRequest="150"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
/>
<StackLayout
Spacing="1"
Margin="8"
>
<Label
FontFamily="{StaticResource MontserratMedium}"
TextColor="Black"
Text="{Binding Title}">
</Label>
<StackLayout
Orientation="Horizontal"
>
<Label
FontFamily="{StaticResource MontserratMedium}"
FontSize="13"
TextColor="{StaticResource TextColorDate}"
Text="Price">
</Label>
<Label
FontFamily="{StaticResource MontserratBold}"
FontSize="13"
HorizontalOptions="EndAndExpand"
TextColor="{StaticResource TextColorCash}"
Text="{Binding Price,StringFormat='{0}K'}">
</Label>
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
Is there solution to have Image with property Aspect="AspectFill" and with rounded corners?

xamarin forms: Circle image is not working in xamarin forms ios(Showing as oval shape) [duplicate]

This question already has an answer here:
Xamarin forms: Image is not showing in perfect circle
(1 answer)
Closed 4 years ago.
For the circle images, I am using Xam.Plugins.Forms.ImageCircle nuget package in my project, which is working fine in android and windows but showing an oval shape in IOS, screenshot adding below.
Added ImageCircleRenderer.Init(); in AppDelegate.cs
xmlns namespace added:
xmlns:controls="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions"
<ListView x:Name="MyTweetsTopics"
ItemsSource="{Binding AllItems,Mode=TwoWay}"
IsPullToRefreshEnabled="True"
HasUnevenRows="True"
RefreshCommand="{Binding RefreshCommand}"
IsRefreshing="{Binding IsRefreshing}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="5"
Padding="5"
Orientation="Horizontal">
<controls:CircleImage
HorizontalOptions="Start"
VerticalOptions="Start"
WidthRequest="50"
BorderColor="#1C7DB4"
Aspect="AspectFill"
BorderThickness="2"
HeightRequest="50">
<Image.Triggers>
<DataTrigger TargetType="Image" Binding="{Binding isProfileImageNull}" Value="True">
<Setter Property="Source" Value="dummy_profile.jpg"/>
</DataTrigger>
<DataTrigger TargetType="Image" Binding="{Binding isProfileImageNull}" Value="False">
<Setter Property="Source" Value="{Binding thumbnailImageUrl, Converter={StaticResource urlJoinConverter}}"/>
</DataTrigger>
</Image.Triggers>
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ShowTopicsProfile"
CommandParameter="{Binding .}"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</controls:CircleImage>
<StackLayout
HorizontalOptions="FillAndExpand"
Orientation="Vertical">
<StackLayout
HorizontalOptions="FillAndExpand"
Orientation="Horizontal">
<Label
Text="{Binding pageTitle}"
x:Name="pageTitle"
Font="Bold,17"
TextColor="Black"
HorizontalOptions="Start"
VerticalOptions="Center"/>
<StackLayout
Margin="0,10,0,0"
HorizontalOptions="EndAndExpand"
Orientation="Horizontal"
VerticalOptions="CenterAndExpand">
<Image
WidthRequest="20"
HeightRequest="20"
Margin="0,-5,0,0"
VerticalOptions="Center"
Source="ic_action_time.png"/>
<Label
Text="{Binding pageUpdatedDate, Converter={StaticResource cnvDateTimeConverter}}"
x:Name="tweetedTime"
Font="10"
Margin="-5,-5,0,0"
TextColor="Black"
HorizontalOptions="FillAndExpand"
VerticalOptions="Center"/>
</StackLayout>
</StackLayout>
<Label
Text="{Binding modifier.fullname, StringFormat='Last modified by {0:F0}'}"
Font="10"
TextColor="Black"
HorizontalOptions="Start"
VerticalOptions="Center"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Label/>
</ListView.Footer>
</ListView>
Why in IOS only it is not working?
Thanks in advance
Could you give me your xml (listview) code so that I will able to solve your problem or if want to show Image from Cache you need to use FFImageLoading dll, it is available on nuget.
class CircleImage : CachedImage
{
public CircleImage()
{
LoadingPlaceholder = Glyphs.ImagePlaceholder;
ErrorPlaceholder = Glyphs.ImagePlaceholder;
DownsampleToViewSize = true;
Transformations = new List<FFImageLoading.Work.ITransformation>();
Transformations.Add(new CircleTransformation());
}
}
Use above control.

Resources