如何将Xamarin Forms视图转换为具有相同大小的本机视图iOS/Android? [英] How to convert Xamarin Forms view to a Native view iOS/Android with the same size?
本文介绍了如何将Xamarin Forms视图转换为具有相同大小的本机视图iOS/Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里使用示例,但是我正在努力在此处填写正确的size参数,以便它表示与表单视图相同的大小. https://michaelridland.com/xamarin/creating-native-view -xamarin-forms-viewpage/
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;
}
对于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;
}
}
应该输入Xamarin表单中的哪些值,以便它将代表在本地视图上具有相同值所需的CGRect.
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.
推荐答案
var rect = new CGRect();
rect.X = (nfloat)view.X;
rect.Y = (nfloat)view.Y;
rect.Width = (nfloat)view.Width;
rect.Height = (nfloat)view.Height;
解决了此问题,其中视图"是Xamarin Forms视图.
Solved it, where "view" is the Xamarin Forms view.
这篇关于如何将Xamarin Forms视图转换为具有相同大小的本机视图iOS/Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文