我创建了一个自定义控件,以在应用程序中创建导航窗格,该应用程序具有内容区域和滑动面板,该滑动面板从Xamarin.iOS中的页面边缘滑出,使用的是UIView,我需要为相同。首先,我创建了一个视图,将在内容区域中添加元素,并将其转换为iOS渲染器中的本机视图。然后,我尝试通过滑动器导航添加到内容视图中的元素,但该元素没有移动,但是在触摸元素时会聚焦。我已将 isAccessibilityElement设置为false,以使内容视图可聚焦。滑动时我需要使元素可访问。有人可以帮忙吗?并在此先感谢。请找到以下代码:
简单的示例代码段:
<drawer:CustomControl x:Name="drawer">
<drawer:CustomControl.ContentView>
<StackLayout Margin="0,100,0,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Button Text="Button" />
<Label Text="ContentView"/>
<Button Text=" Button2"/>
<Label Text="ContentView2"/>
</StackLayout>
</drawer:CustomControl.ContentView>
</drawer:CustomControl>
本机代码段:
public class SimpleControl : UIView
{
UIView contentView;
public UIView ContentView
{
get
{
return this.contentView;
}
set
{
this.contentView = value;
this.Add(this.contentView);
}
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
this.IsAccessibilityElement = false;
}
}
Xamarin.Forms
public class CustomControl: View
{
public View ContentView
{
get { return (View)GetValue(ContentViewProperty); }
set { SetValue(ContentViewProperty, value); }
}
public static readonly BindableProperty ContentViewProperty = BindableProperty.Create(nameof(ContentView), typeof(View), typeof(CustomControl), null, BindingMode.Default);
public CustomControl()
{
}
protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
{
if (Double.IsNaN(widthConstraint) || Double.IsNaN(heightConstraint)
|| Double.IsInfinity(widthConstraint) || Double.IsInfinity(heightConstraint))
{
SizeRequest size = base.OnMeasure(widthConstraint, heightConstraint);
widthConstraint = size.Request.Width;
heightConstraint = size.Request.Height;
}
return new SizeRequest(new Size(widthConstraint, heightConstraint));
}
}
iOS渲染器:
public class CustomControlRenderer : ViewRenderer<CustomControl, Native>
{
public CustomControlRenderer()
{
}
Native nativeDrawer;
CustomControl drawer;
protected override void OnElementChanged(ElementChangedEventArgs<CustomControl> e)
{
base.OnElementChanged (e);
if (e.NewElement != null) {
drawer = e.NewElement;
if (nativeDrawer == null)
{
nativeDrawer = new Native ();
UIView v = new UIView();
v.Frame = new CGRect(0, 0, 600, 1500);
nativeDrawer.ContentView = CustomControlRenderer.ConvertFormsToNativeView(drawer.ContentView, v.Frame, drawer, drawer.BindingContext);
}
SetNativeControl(nativeDrawer);
}
}
internal static UIView ConvertFormsToNativeView(Xamarin.Forms.View view, CGRect size, CustomControl drawer, object bindingContext)
{
if (view == null)
return null;
if (view.BindingContext == null)
{
if (bindingContext != null)
{
view.BindingContext = bindingContext;
}
else
{
view.BindingContext = drawer.BindingContext;
}
}
return GetNativeView(view, drawer, view.BindingContext, 0, 0, size.Width, size.Height);
}
}