Xamarin.Forms: Rendering Xamarin.Froms Control in native Dialog - wrap_content ignored

Viewed 957

Say I want to render a Xamarin.Froms control (e.g a Xamarin.Forms.Label) in a native android view.
The following code works. But ignores the width and height of the control (wrap_content).

// create a Xamarin.Froms.Label
Xamarin.Forms.Label view = new Xamarin.Forms.Label()
{
    Text = "This is a Xamarin.Forms.Label",
    BackgroundColor = Color.Red,
};

// create a Renderer
var renderer = Xamarin.Forms.Platform.Android.Platform.CreateRenderer(view);

// create an AlertDialog
var builder = new AlertDialog.Builder(Xamarin.Forms.Forms.Context);
builder.SetView(renderer.ViewGroup); // Use the ViewGroup of the renderer
builder.SetTitle("Dialog");
// create and show the dialog
builder.Create().Show();

Produces the following:

Xamarin.Forms.Label screenshot

Doing the same with an native label:

var view = new Android.Widget.TextView(Xamarin.Forms.Forms.Context)
{
    Text = "This is a Android.Widget.TextView",
    Background = new Android.Graphics.Drawables.ColorDrawable(Android.Graphics.Color.Red),
};
var builder = new AlertDialog.Builder(Xamarin.Forms.Forms.Context);

builder.SetView(view);
builder.SetTitle("Dialog");
builder.Create().Show();

Produces the expected output:

Android.Widget.TextView screenshot

I am looking for any hints or advices regarding width / height measurement in Xamarin.Forms, and/or a solution for this kind of problem.


Update

Also note that this does not depend on the AlertDialog, using a LinearLayout instead of a AlertDialog results to the same behaviour.

2 Answers
Related