How to convert Xamarin Forms view to a Native view iOS/Android with the same size?

匆匆过客 提交于 2020-06-17 09:31:07

问题


I am using the example here, but I am struggeling to fill in the right size parameter here so that it represents the same size as the forms view. https://michaelridland.com/xamarin/creating-native-view-xamarin-forms-viewpage/

    public static UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
    {
        var renderer = RendererFactory.GetRenderer(view);

        renderer.NativeView.Frame = size;

        renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
        renderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill;

        renderer.Element.Layout(size.ToRectangle());

        var nativeView = renderer.NativeView;

        nativeView.SetNeedsLayout();

        return nativeView;
    }

And for Android:

public static ViewGroup ConvertFormsToNative(Xamarin.Forms.View view, Rectangle size)
{
    var vRenderer = RendererFactory.GetRenderer (view);
    var viewGroup = vRenderer.ViewGroup;
    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.WidthRequest, (int)view.HeightRequest);
    return viewGroup; 
}

}

What values from my Xamarin Forms should be put in, so that it will represent the CGRect required to have the same on the Native View.

来源:https://stackoverflow.com/questions/62412402/how-to-convert-xamarin-forms-view-to-a-native-view-ios-android-with-the-same-siz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!