Xamarin.Forms: Add item to certain row of grid from code - xamarin.forms

in my XAML I have this grid:
<Grid Grid.Row="12" Grid.Column="1" ColumnSpacing="0" >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<!--Label-->
<RowDefinition Height="3*" ></RowDefinition>
<!--ProfilePic-->
<RowDefinition Height="*"></RowDefinition>
<!-- Btn change-->
</Grid.RowDefinitions>
<BoxView BackgroundColor="Purple" Grid.Row="1"></BoxView>
<ImageButton
Grid.Row="2"
x:Name="row_forpic_editProfile"
HorizontalOptions="Center"
VerticalOptions="Center"
/>
<Label
Grid.Row="0"
FontSize="16"
TextColor="#272727"
VerticalOptions="End"
Text="Proilfbild"
FontFamily="arial"
/>
<Button
Grid.Row="2"
x:Name="btn_changepic_editprofile"
Text="Ändern"
HorizontalOptions="Center"
VerticalOptions="Center"
/>
</Grid>
This is the result of that:
The result
Now, I want to add an imageview into the purple row (which is Grid.Row="1") from code because I downloaded a picture from the internet.
How can I now add my new Image (ImageButton) into this exact row? (row 1)
And also have it maintain its dimensions (only the picture must full the container).
thank you:)

to add a View to a Grid
myGrid.Children.Add(view,col,row);

Related

How align elements in Xamarin StackLayout?

I am attaching the result of this code.
enter image description here
What I want the layout to look like is that the red stays centered and the yellow goes towards the ends of the Horizontal.
And regardless of the text size of the label inside the yellow, I want the red color to remain centered.
please give me advice
<StackLayout
BackgroundColor="White"
HorizontalOptions="Center"
Orientation="Horizontal">
<Label x:Name="xTitle"
BackgroundColor="Red"
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
Text="asdasdasdas"
VerticalOptions="Fill" />
<Label x:Name="xResult"
BackgroundColor="Goldenrod"
HorizontalOptions="End"
HorizontalTextAlignment="Center"
Text="123123123123123"
VerticalOptions="Fill" />
</StackLayout>
I tried to send the yellow part to the far end while keeping the red label in the center.
You can try to use 'Grid' to achieve this, just as ToolmakerSteve mentioned.
Please refer the following code: 
<StackLayout>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="1"
x:Name="xTitle"
BackgroundColor="Red"
Text="asdasdasdas"
VerticalOptions="Fill"
HorizontalOptions="Center" />
<Label Grid.Row="0"
Grid.Column="2"
x:Name="xResult"
Text="123123123123123"
BackgroundColor="Goldenrod"
HorizontalTextAlignment="Center"
VerticalOptions="Fill"
HorizontalOptions="End"/>
</Grid>
</StackLayout>
For more information, you can check Xamarin.Forms Grid.

xamarin.forms: View isnt fullscren on iOS borders (slight corner at the edges)

I am trying to make the top carouselview fullscreen. However, there is a slight border to the left and to the upper part of the every picture in the carouselview. If I set a background color to the grid, it gets rendered into the corners, but not with the picture. This code works on Android, the problem exists only on iOS. Any Ideas?
<Grid>
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions >
<RowDefinition Height="285"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" >
<CarouselView VerticalOptions="Start" Margin="0,0,0,0" HorizontalOptions="Start" Loop="false" x:Name="carouselview" ItemTemplate="{StaticResource templateSelector}" />
</Grid>
The template selector:
<Grid RowSpacing="0" HeightRequest="280">
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Grid.GestureRecognizers>
<Image
Source="{Binding fullPic}"
Aspect="Fill"
x:Name="img_pic"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand" />
<Grid Margin="0,0,4,4"
HorizontalOptions="End"
VerticalOptions="End">
<BoxView
CornerRadius="20"
IsVisible="{Binding isNumberVisible}"
BackgroundColor="#99000000"/>
<Label
Text="{Binding number}"
FontSize="15"
VerticalOptions="Center"
HorizontalOptions="Center"
FontFamily="Font_11"
TextColor="White"
/>
</Grid>
</Grid>
Adding this to the main grid of the datatemplate of the carouselview fixed the issue:
Margin="-2"

Overlapping frame in a grid xamarin forms

I am using the latest xamarin forms.
I have a requirement that I need to position a frame between 2 rows in a grid.
I could not make it work with absolute layout or flex layout. Below is my simplified attempt with a grid
<Grid ColumnSpacing="1" RowSpacing="1">
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="8*"/>
</Grid.RowDefinitions>
<Grid BackgroundColor="Red" Grid.Row="0">
<Label Text="Add some stuff here"></Label>
</Grid>
<Grid BackgroundColor="Blue" Grid.Row="1">
<Frame BackgroundColor="Green" HeightRequest="100" WidthRequest="100" Margin="20,-150,20,20"></Frame>
</Grid>
</Grid>
This is my UNWANTED RESULT
I cannot seem to set the heighrequest of the frame as it remains big.
What is the best approach to overlay a frame between 2 rows?
thanks
For your layout, you can use AbsoluteLayout in the following manner:
<AbsoluteLayout BackgroundColor="Blue" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Pink" AbsoluteLayout.LayoutBounds="1,0,1,0.5" AbsoluteLayout.LayoutFlags="All" />
<StackLayout AbsoluteLayout.LayoutBounds="1,1,1,0.5" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Beige"/>
<Frame AbsoluteLayout.LayoutBounds="0.5,0.5,0.8,0.5" AbsoluteLayout.LayoutFlags="All" BackgroundColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" CornerRadius="20" />
</AbsoluteLayout>
You will get the following design from the above implementation.

Xamarin Forms: Not able to create a corner radius for a two column layout

I'm trying to create a grid layout with a height of 8px with cornered radius horizontally as below snapshot.
: https://ibb.co/s1dkpnw
<Grid Margin="0,0,0,24" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*">
</ColumnDefinition>
<ColumnDefinition Width="4*">
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="8">
</RowDefinition>
</Grid.RowDefinitions>
<Frame CornerRadius="8" Grid.Row="0" HorizontalOptions="FillAndExpand" HeightRequest="8">
<Label Grid.Row="0" Grid.Column="0" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Lime"></Label>
<Label Grid.Row="0" Grid.Column="1" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Blue"></Label>
</Frame>
</Grid>
I'm able to get the UI as in the below snapshot.
: https://ibb.co/7nLTgdL
I'm not able to get the UI displyed within the frame visible, although i'm getting a cornered radius for a two columned layout. It just shows a empty frame.
Please let me know how to design the XAML UI to get the UI as in the design snapshot.
Got the UI aligned by pushing the Grid within the frame control as below,
<Frame CornerRadius="8" Grid.Row="0" HorizontalOptions="FillAndExpand" HeightRequest="8">
<Grid HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*">
</ColumnDefinition>
<ColumnDefinition Width="4*">
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="8">
</RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Lime"></Label>
<Label Grid.Row="0" Grid.Column="1" HeightRequest="8" HorizontalOptions="FillAndExpand" BackgroundColor="Blue"></Label>
</Grid>
</Frame>
AbsoluteLayout can help you:
<AbsoluteLayout HorizontalOptions="Center">
<Frame
AbsoluteLayout.LayoutBounds="0,0,150,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#0086C9"
CornerRadius="7" />
<Frame
AbsoluteLayout.LayoutBounds="120,0,150,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#D8D8D8"
CornerRadius="7" />
<Frame
AbsoluteLayout.LayoutBounds="120,0,30,8"
AbsoluteLayout.LayoutFlags="None"
BackgroundColor="#0086C9"
CornerRadius="1" />
</AbsoluteLayout>
Output: https://i.ibb.co/jh6QmBL/ss.png

Change grid layout at runtime

I have a grid with the following row definitions...
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
When the user presses a button, how do I remove the bottom two rows and have the top row (including the view within it) stretch to the bottom of the grid?
I kept all the rows and did this...
mainGrid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
mainGrid.ForceLayout();
I would like to animate the change in height.
Try something like this:
The grid
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkOne"
BackgroundColor="Red"/>
<StackLayout Grid.Row="1"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkTwo"
BackgroundColor="Green"/>
<StackLayout Grid.Row="1"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="stkThree"
BackgroundColor="Blue"/>
<Button Text="Expand One"
HorizontalOptions="EndAndExpand"
Command="ExpandCommand"/>
</Grid>
The code behind
// Create the command and set it up to execute this method:
void ExecuteExpand()
{
stkTwo.IsVisible=false;
stkThree.IsVisible=false;
Grid.SetRowSpan(stkOne, 3);
grid.ForceLayout();
}
I'm not sure if it will work as you need, but make a try.

Resources