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


say want render xamarin.froms control (e.g xamarin.forms.label) in native android view.
following code works. ignores width , height of control (wrap_content).

// create xamarin.froms.label xamarin.forms.label view = new xamarin.forms.label() {     text = "this xamarin.forms.label",     backgroundcolor = color.red, };  // create renderer var renderer = xamarin.forms.platform.android.platform.createrenderer(view);  // create alertdialog var builder = new alertdialog.builder(xamarin.forms.forms.context); builder.setview(renderer.viewgroup); // use viewgroup of renderer builder.settitle("dialog"); // create , show dialog builder.create().show(); 

produces following:

xamarin.forms.label screenshot

doing same native label:

var view = new android.widget.textview(xamarin.forms.forms.context) {     text = "this 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 expected output:

android.widget.textview screenshot

i looking hints or advices regarding width / height measurement in xamarin.forms, and/or solution kind of problem.


update

also note not depend on alertdialog, using linearlayout instead of alertdialog results same behaviour.

the following code works. ignores width , height of control (wrap_content).

reason:

you passed viewgroup alertdialog. inside alertdialog layout params of viewgroup won't work @ all. refering this blog:

every rule has exception

...

the issue here alertdialog.builder supports custom view, not provide implementation of setview() takes layout resource; must inflate xml manually. however, because result go dialog, not expose root view (in fact, doesn’t exist yet), not have access eventual parent of layout, cannot use inflation. turns out, irrelevant, because alertdialog erase layoutparams on layout anyway , replace them match_parent.

so instead of passing viewgroup, pass view alertdialog second try.

update(a few more research):

if try following codes set height of viewgroup 400:

// create renderer var renderer = xamarin.forms.platform.android.platform.createrenderer(view); // create alertdialog var builder = new alertdialog.builder(xamarin.forms.forms.context); renderer.viewgroup.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.wrapcontent, 400); builder.setview(renderer.viewgroup); // use viewgroup of renderer builder.settitle("dialog"); // create , show dialog builder.create().show(); 

it doesn't work, dialog still covers whole screen:

enter image description here

but if add renderered viewgroup linearlayout this:

var renderer = xamarin.forms.platform.android.platform.createrenderer(view); //the following codes works correctly renderer.viewgroup.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.wrapcontent, 400); linearlayout linearlayout = new linearlayout(xamarin.forms.forms.context); linearlayout.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent); linearlayout.addview(renderer.viewgroup); this.addcontentview(linearlayout,new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent)); 

the height set correctly:

enter image description here

so, on layer, problem lies in alertdialog mentioned above.

but if height set wrap_content this:

var renderer = xamarin.forms.platform.android.platform.createrenderer(view); //set height again wrap_content renderer.viewgroup.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.wrapcontent, viewgroup.layoutparams.wrapcontent); linearlayout linearlayout = new linearlayout(this); linearlayout.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent); linearlayout.addview(renderer.viewgroup); this.addcontentview(linearlayout,new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent)); 

the rendererd viewgroup again start fill whole screen:

enter image description here

on layer found problem lies in rendered viewgroup, because after changing viewgroup native linearlayout fine.

i checked source codes of xamarin.forms, , found following inherit hierarchy:

xamarin.forms.platform.androidlabelrenderer -> xamarin.forms.platform.android.viewrenderer<tview, tnativeview> -> xamarin.forms.platform.android.visualelementrenderer<telement> -> xamarin.forms.platform.android.formsviewgroup -> android.views.viewgroup 

i found none of these classes has implemented viewgroup.onmeasure method. wrap_content never work. can refer custom viewgroup detailed implementation example of custom viewgroup.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -