0

I need to get HeightRequest(RelativeLayout) points by real size image. I get image from the server. It is size 720x250. I need to use RelativeLayout.

<ListView
                x:Name="EventsListView"
                GroupDisplayBinding="{Binding EventGroupTitle}"
                HasUnevenRows="True"
                IsGroupingEnabled="True"
                ItemTapped="ListView_OnItemTapped"
                ItemsSource="{Binding GroupedItems}"
                SeparatorVisibility="None">
                <ListView.GroupHeaderTemplate>
               </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell BindingContextChanged="BindableObject_OnBindingContextChanged">

                            <RelativeLayout x:Name="RelativeLayout" Padding="0">
                                <!--  Background picture  -->
                                <controls:BExtendedCachedImage
                                    x:Name="Picture"
                                    Aspect="AspectFit"
                                    CacheType="Disk"
                                    DownsampleToViewSize="True"
                                    LoadingPlaceholder="backgroundBlur"
                                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToView,
                                                                                           ElementName=RelativeLayout,
                                                                                           Property=Height}"
                                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToView,
                                                                                          ElementName=RelativeLayout,
                                                                                          Property=Width}"
                                    Source="{Binding Picture}" />
                            </RelativeLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Source="{Binding Picture}" - my picture from the server. In code-behind in BindableObject_OnBindingContextChanged I need to change HeightRequest (RelativeLayout). But if I do not set the height manually, the cell will stretch to full height.

Here is my code-behind:

private void BindableObject_OnBindingContextChanged(object sender, EventArgs e)
    {
        var cell = (ViewCell) sender;
        var layout = cell.View.FindByName<RelativeLayout>("RelativeLayout");
        var bc = (GroupEventsMapPageModel) BindingContext;
        layout.HeightRequest = THIS_MANUAL_VALUE;
    }

How I can to get THIS_MANUAL_VALUE?

I tried via this code:

var device = Resolver.Resolve<IDevice>();
        var display = device.Display;
        return display.Scale;

Image Height (250) \ display.Scale - doesn't work. I create tab.

xxxhdpi device (640 dpi) - 4 (Scale) - layout.HeightRequest = 79;
xxxhdpi device (560 dpi) - 3.5 (Scale) - layout.HeightRequest = 93;
xhdpi device (315 dpi) - 2 (Scale) - layout.HeightRequest = 110;
hdpi device (240 dpi) - 1.5 (Scale) - layout.HeightRequest = 165;

However, I noticed that when changing the diagonal.

4.7" xxhdpi device (420 dpi) - 2.625 (Scale) - layout.HeightRequest = 87;
5,2" xxhdpi device (420 dpi) - 2.625 (Scale) - layout.HeightRequest = 130;

How can I get the formula?

halfer
  • 19,824
  • 17
  • 99
  • 186
Igor Strekha
  • 176
  • 3
  • 17

1 Answers1

0

If you're looking to set the height:

StackLayout _mainStack = new StackLayout{ HeightRequest = 40};

if you're looking to get the height:

var _stacksHeight = _mainStack.Height;

The first gets or sets the desired height override for this element. The second is a read only property to get the rendered height of _mainStack.

Joshua Poling
  • 561
  • 8
  • 29