xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

Layout not rendered properly inside the AbsoluteLayout while set LayoutOption #8545

Open Sasikumar3595 opened 4 years ago

Sasikumar3595 commented 4 years ago

Description

In our custom control, we are providing the DataTemplate property for view customization support. But the template does not rendering properly.

If we set the HorizontalOptions or VerticalOptions to label inside the AbsoluteLayout then the empty view shown. else the template rendered properly.

Steps to Reproduce

  1. Run the attached sample
  2. Click open button to open the view
  3. DataTemplate does not rendered properly

Expected Behavior

Custom View(DataTemplate) rendered properly

Actual Behavior

Custom View(DataTemplate) rendered properly

Basic Information

Screenshots

Issue:

Ouput Image

Reproduction Link

Sample Link : CustomControl (4).zip

Code Snippet for converting Xamarin forms view to Native forms view

////  This is the code snippet for converting Xamarin forms view to Native forms view

view = (Xamarin.Forms.View)template.CreateContent();
var vRenderer = Convert(view, (Xamarin.Forms.VisualElement)formsView.Parent, nativeControl.Context);
var viewGroup = vRenderer.View;
vRenderer.Tracker.UpdateLayout();
var layoutParams = new ViewGroup.LayoutParams((int)size.Width, (int)size.Height);
viewGroup.LayoutParameters = layoutParams;
view.Layout(size);
viewGroup.Layout(0, 0, (int)view.Width, (int)view.Height);
view.Measure(double.PositiveInfinity, double.PositiveInfinity);

////  This is the code snippet for converting Xamarin forms view to IVisualElementRenderer

internal static IVisualElementRenderer Convert(Xamarin.Forms.View source, Xamarin.Forms.VisualElement valid, Context context)
{
var render = Platform.GetRenderer(source);
render = Platform.CreateRendererWithContext(source, context);
Platform.SetRenderer(source, render);
return render;
}

In Sample

Set HorizontalOptions or VerticalOptions inside the AbsoluteLayout makes issue eg.,

<AbsoluteLayout BackgroundColor="Transparent" 
                                        Margin="0" 
                                        Padding="1">
<Label FontSize="13"
                                   BackgroundColor="Blue"
                                   AbsoluteLayout.LayoutBounds="0.2,0.2,1,1" 
                                   AbsoluteLayout.LayoutFlags="All" 
                                   FontAttributes="None" 
                                   Text="{Binding TextValue}"  
                                   VerticalTextAlignment="Center" 
                                   HorizontalTextAlignment="Center" 
                                   HorizontalOptions="Center"
                                   VerticalOptions="Center"
                                   TextColor="Red" />
 </AbsoluteLayout>

If we does not set HorizontalOptions or VerticalOptions then it does not reproduced eg.,

<AbsoluteLayout BackgroundColor="Transparent" 
                                        Margin="0" 
                                        Padding="1">
<Label FontSize="13"
                                   BackgroundColor="Blue"
                                   AbsoluteLayout.LayoutBounds="0.2,0.2,1,1" 
                                   AbsoluteLayout.LayoutFlags="All" 
                                   FontAttributes="None" 
                                   Text="{Binding TextValue}"  
                                   VerticalTextAlignment="Center" 
                                   HorizontalTextAlignment="Center" 
                                   TextColor="Red" />
 </AbsoluteLayout>
Sasikumar3595 commented 4 years ago

@hartez , Any update on this?

Sasikumar3595 commented 4 years ago

@hartez , Any update on this?

hartez commented 4 years ago

@Sasikumar3595 No. If anything changes with this issue, it will be updated here.

Question, though - in your method for converting a Forms element to an IVisualElementRenderer:

internal static IVisualElementRenderer Convert(Xamarin.Forms.View source, Xamarin.Forms.VisualElement valid, Context context)
{
    var render = Platform.GetRenderer(source);
    render = Platform.CreateRendererWithContext(source, context);
    Platform.SetRenderer(source, render);
    return render;
}

Why do you call var render = Platform.GetRenderer(source); and immediately overwrite the result with render = Platform.CreateRendererWithContext(source, context);?

Sasikumar3595 commented 4 years ago

@hartez ,

render = Platform.GetRenderer(source); as not needed.

Replace the following code

var render = Platform.GetRenderer(source);
render = Platform.CreateRendererWithContext(source, context);

with var render = Platform.CreateRendererWithContext(source, context);

Note: Is this issue raised due to this conversion?

when will the issue resolved?

Sasikumar3595 commented 4 years ago

@hartez , Any update on this?

Sasikumar3595 commented 4 years ago

@hartez, Any update on this?

Sasikumar3595 commented 4 years ago

@hartez / @samhouts Any update on this?