image - 在android和ios xamarin表单中单击或点击图像时如何显示数字(1,2,3)
问题描述
如何在单击该位置以在图像中显示数字(例如(1,2,3,4)UI 级别)时生成生成 ID 是否有机会在 xamarin 或渲染器中的 Android 和 ios 中实现。谢谢提前
当我单击特定位置时,我正在使用 Sfimageeditor,点击获取 x 和 y 位置,基于我必须显示该位置上的数字
Xaml 代码
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<local:CustomImage x:Name="Img" TapEvent="Img_TapEvent" Source="{Binding PhotoSource}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFit">
<local:CustomImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ImageTapCommand}" />
</local:CustomImage.GestureRecognizers>
</local:CustomImage>
</StackLayout>
单击 PhotoSource 并保存以在图像的 UI 级别中生成 id 显示
Android 自定义图像渲染器:
public bool OnTouch(Android.Views.View v, MotionEvent e)
{
float Xaxis = e.GetX();
flaot Yaxis = e.GetY();
return false;
}
ios 自定义图像渲染器:
public void TapHandler(UITapGestureRecognizer tgr)
{
CGPoint touchPoint = tgr.LocationInView(nativeElement);
formsElement.OnTapEvent(float Xaxis = (float)touchPoint.X, float Yaxis = (float)touchPoint.Y);
}
解决方案
你可以使用 relativelayout 来设置图片的控件,它有
XConstraint,Constraint 类型,它是一个附加属性,表示对子项 X 位置的约束。
YConstraint,Constraint 类型,它是一个附加属性,表示对子项 Y 位置的约束。
这是一个例子:
xml:
<RelativeLayout x:Name="rel">
<StackLayout>
<Image Source="mypic.jpg" x:Name="pic">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="on_Tapped"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
</RelativeLayout>
代码隐藏:
private void on_Tapped(object sender, EventArgs e)
{
rel.Children.Add(new Label
{Text="1",
TextColor=Color.Red
}, () => new Rectangle(0, 10, 20, 20));//here is where you want to add the label
}
有关它的更多信息,
你可以参考这个
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relativelayout
推荐阅读
- react-native - RNPickerSelect 试图从数组中对象的映射数据中显示选择
- r - 如何停止闪亮仪表板的自动关闭
- laravel - 如何修复 laravel 中的“未定义变量:img_name”错误
- c++ - 逐段、逐行读取单词 (C++)
- javascript - 将拖动的文件附加到 div
- reporting-services - 如何在 SSRS 报告上打印 ★ 符号
- c - 如果当前用户的用户名包含 Unicode 字符,getenv("APPDATA") 将返回不正确的值
- php - 在角色之后过滤用户
- swift - tableView.selectRow has no effect when called within a UIContextualAction callback
- python - 正在发送的 python 邮件有一个空白的 TO 字段