Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How can I manually render a custom view from Xamarin.Forms into a native view?

$
0
0

I've been rewriting some parts of my app to use custom renderers to avoid some of the Xamarin.Forms bugs. For example I had a problem with a ListView headers, so I switched to using a ViewRenderer with UITableView on iOS.

The question is, given that I have already written the ViewCell in Xamarin.Forms, is there a way to reuse that in the iOS specific renderer, and just insert the view into the iOS hierarchy? For example

public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    return MyViewCell.Create();
}

class MyViewCell : UITableViewCell {
    public static MyViewCell Create(FoodMenuItem food)
    {
        var view = (MyViewCell)Nib.Instantiate(null, null)[0];

        // HERE I WANT TO USE A VIEW FROM XAMARIN FORMS
        // and attach it via view.AddSubview() or something else.

        return view;
    }
}

I basically just need a way to turn a Xamarin.Forms.View into a UIView on iOS and an Android View on Android. I've found that I could do something like

var renderer = new LabelRenderer();
renderer.SetElement(new Label { Text = "foobar" });
// now use renderer.NativeView;

But I'm not sure if this is the right way to do it?


Viewing all articles
Browse latest Browse all 58056

Trending Articles