【问题标题】:InputTransparent=true does not work in Xamarin Forms AndroidInputTransparent=true 在 Xamarin Forms Android 中不起作用
【发布时间】:2016-11-02 11:38:50
【问题描述】:
来自this 链接。
如果元素应该接收输入,则返回 false;如果元素不应该接收输入并且应该将输入传递给下面的元素,则为 true。默认为假。
我想要的是,不得允许 Entry 字段接收来自用户的输入。
InputTransparent=true 在 iOS 中运行良好,但在 Android 中无法运行,它仍然允许用户提供输入。
我尝试了IsEnabled=false,但这改变了我的输入字段的外观,我不希望这样。
这是某种错误吗?
【问题讨论】:
标签:
xamarin
xamarin.ios
xamarin.android
【解决方案1】:
InputTransparent 不适用于 Android。我为 StackLayout 创建了简单的渲染:
在 PCL 项目中:
public class StackLayoutAdd :StackLayout
{
}
在 Android 项目中:
[assembly: ExportRenderer(typeof(StackLayoutAdd), typeof(StackLayoutAddCustom))]
.....
public class StackLayoutAddCustom : VisualElementRenderer<StackLayout>
{
public override bool DispatchTouchEvent(MotionEvent e)
{
base.DispatchTouchEvent(e);
return !Element.InputTransparent;
}
}
我在我的 xaml 中使用它:
<StackLayoutAddCustom InputTransparent={Binding IsReadOnly}>
<Editor />
....
</StackLayoutAddCustom>
这是儿童控制的工作。
【解决方案2】:
基于this question:
这会在触摸屏幕时连续触发,因此应用自定义渲染器并覆盖 DispatchTouchEvent
public override bool DispatchTouchEvent(MotionEvent e)
{
if (Element.InputTransparent)
{
return false;
}
return base.DispatchTouchEvent(e);
}