I am using observable collection and displaying my images in carousel view. I need to adjust them by size..for example the first the biggest and then the images should go smaller. Is there a way to achieve this?
<IndicatorView x:Name="indicatorView" IndicatorSize="14"
MaximumVisible="6" IndicatorsShape="Circle"
Margin="0,20,0,30"
IndicatorColor="LightGray"
SelectedIndicatorColor="DarkGray"
HorizontalOptions="Center">
</IndicatorView>
<!--Pictures-->
<CarouselView Margin="0,0,0,20" HorizontalScrollBarVisibility="Never" x:Name="image"
ItemsSource="{Binding ImageResults, Mode=TwoWay}"
BackgroundColor="{DynamicResource PictureBackground}"
IndicatorView="indicatorView" >
<CarouselView.ItemTemplate >
<DataTemplate>
<Image Source="{Binding FieldVisualData}" WidthRequest="530" HeightRequest="334" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
I find that Image uses the WidthRequest and HeightRequest.
There is a easy way to make the size of the images in CarouselView consistent, you could set AspectFit for each Image to show the same effects.
For example:
<Image Source="{Binding FieldVisualData}"
WidthRequest="530"
HeightRequest="334"
Aspect="AspectFit"/>
I am trying to get an image on the bottom right corner. I can get it easily but the problem now is to change the size of the image. I want to preserve the ratio of image while setting either height or width.
<StackLayout Grid.Column="0">
<Image Source="party.png"
HeightRequest="100"
VerticalOptions="EndAndExpand"
Aspect="AspectFit"/>
</StackLayout>
With the xaml above, I set height and width is being changed accordingly. But, when I add HorizontalOptions="EndAndExpand" in the image i lost the aspect ratio.
How can I put image at bottom right with preserving the aspect ratio?
You can use HorizontalOptions="End" , then image will at bottom right with preserving the aspect ratio .
<StackLayout Grid.Column="0" BackgroundColor="LightBlue">
<Image Source="Saimiri_sciureus.jpg"
HeightRequest="100"
VerticalOptions="EndAndExpand"
HorizontalOptions="End"
Aspect="AspectFit" />
</StackLayout>
The effect :
You can try:
<StackLayout Grid.Column="0" BackgroundColor="LightBlue">
<Image Source="party.png"
HeightRequest="*"
VerticalOptions="EndAndExpand"
HorizontalOptions="EndAndExpand"
Aspect="AspectFit" />
And that will keep the aspect ratio.
I'm trying to embed an editor in a Xamarin.Forms.DataGrid cell using following code (inspired from this sample)
<ContentPage.Content>
<StackLayout>
<dg:DataGrid ItemsSource="{Binding Items}" SelectionEnabled="True" SelectedItem="{Binding SelectedItem}"
RowHeight="70" HeaderHeight="50" PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}">
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Col. 1" PropertyName="Prop. 1" Width="0.7*">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Entry Text="{Binding}" />
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
</StackLayout>
</ContentPage.Content>
Now that works ok, but the edited text is aligned top-left of the cell. So I go trying to use one of the following alignment parameters:
HorizontalOptions="Center" VerticalOptions="Center"
<!-- or >
HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
But in both cases, I end up with text centered within the cell, but now, the editor doesn't expand (it seems as if I had padding)... And the result with either Centeror CenterAndExpand is actually the same:
Could anyone help me get the desired result ?
EDIT: updated with full grid's definition
BONUS question: how to get rid of the editor's border?
You can try this:
<Entry Text="{Binding}" HorizontalTextAlignment="Center" />
Bonus answer: To get rid of border you need custom renderers.
I have the following simple XAML page:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.UserGuides.VPN">
<ContentPage.Content>
<ScrollView>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Margin="10,10,10,10">
<Label Margin="10,10,10,10" Text="How to connect to VPN." HorizontalOptions="Center" />
<Label Margin="10,10,10,10" Text="If you have a corporate notebook, look for the Cisco AnyConnect application. Either in your start menu, on your desktop, or in your taskbar." />
<Image x:Name="imgVPN1" Margin="10,10,10,10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
<Label Margin="10,10,10,10" Text="Select your region and press the Connect button." />
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
In the constructor, I call a method to load the images:
private void LoadImages()
{
imgVPN1.Aspect = Aspect.AspectFit;
ImageSource imagesrc = ImageSource.FromResource("Test.Files.CiscoAnyConnect.PNG");
imgVPN1.Source = imagesrc;
}
The result looks like this on my (rather large, high DPI) phone:
imgur link 1
Of course, I want the image to automatically expand so it takes up the entire width of the screen. While maintaining the aspect ratio. But how can I achieve this? I've already tried putting the Aspect to "AspectFill", and added
imgVPN1.WidthRequest = Application.Current.MainPage.Width;
To the LoadImages method, but that makes the end result look like this:
imgur link 2
I've been toying around with various HorizontalOptions, VerticalOptions, Aspects, GridView, StackLayouts,... The end result is always either the first or the second screenshot. So how can I achieve what I want here?
Here is what can be done. XML almost didn't change, I just removed image Layout Options
<ContentPage.Content>
<ScrollView>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Margin="10,10,10,10">
<Label Margin="10,10,10,10" Text="How to connect to VPN." HorizontalOptions="Center" />
<Label Margin="10,10,10,10" Text="If you have a corporate notebook, look for the Cisco AnyConnect application. Either in your start menu, on your desktop, or in your taskbar." />
<Image x:Name="imgVPN1" Margin="10,10,10,10" />
<Label Margin="10,10,10,10" Text="Select your region and press the Connect button." />
</StackLayout>
</ScrollView>
</ContentPage.Content>
After implementing everything in relative layout which also was not simple (I have the code if anybody needs it let me know) I found easier way. The key point is that image doesn't scale correctly and occupies too much space in height. But we know the size ratio of this image so we can use it. In the code we need to override OnSizeAllocated because in constructor the width is unknown yet. Then it's easy
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
imgVPN1.WidthRequest = width;
imgVPN1.HeightRequest = width / 2.152; //given that image is 411 x 191
}
You could use other width than page with if you want. For example you could use stack layout width to consider margins for more accuracy but this is already tuning. Or you actually could use the image width itself like that
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
double needHeight=imgVPN1.Width / 2.152;
if (imgVPN1.Height != needHeight)
imgVPN1.HeightRequest = needHeight;
}
Here is what you need.
<ContentPage.Content>
<ScrollView>
<StackLayout Margin="20" Spacing="10">
<Label Text="How to connect to VPN." HorizontalOptions="Center" />
<Label Text="If you have a corporate notebook, look for the Cisco AnyConnect application. Either in your start menu, on your desktop, or in your taskbar." />
<Image Source="yourimage.jpg" HorizontalOptions="Fill" />
<Label Text="Select your region and press the Connect button." />
</StackLayout>
</ScrollView>
</ContentPage.Content>
I am looking for a way to add a menu to my ToolbarItem. Right now its just clickable text, here is an example of what I am trying to create,
Here is what I have so far:
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding EditButtonText, Mode=TwoWay}" Clicked="EditClicked" />
</ContentPage.ToolbarItems>
You should be able to set the order of the ToolBarItem to Secondary to force the option into an overflow menu on Android:
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding EditButtonText, Mode=TwoWay}" Clicked="EditClicked" Order="Secondary" />
</ContentPage.ToolbarItems>