【问题标题】:x,y coordinates from xamarin.forms image in androidx,y 坐标来自 android 中的 xamarin.forms 图像
【发布时间】:2018-03-20 15:51:29
【问题描述】:

在我的 xamarin.forms 项目中,我得到了一张图片。 此图像有一个看起来像这样的渲染器:

  public class CustomImage : Image
    {
        public event EventHandler<PointEventArgs> TapEvent;
        public void OnTapEvent(int x, int y)
        {
            TapEvent?.Invoke(this, new PointEventArgs(x, y));
        }
    }

Android 渲染器如下所示:

 public class ImageRendererAndroid : ImageRenderer
    {
        private ImageView nativeElement;
        private CustomImage formsElement;

        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                if (Control != null)
                {
                    Control.Clickable = true;
                    Control.SetOnTouchListener(ImageTouchListener.Instance.Value);
                    Control.SetTag(Control.Id, new JavaObjectWrapper<CustomImage> { Obj = Element as CustomImage });
                }
            }
        }
    }

当我调用 Control.setTag 方法时,我收到以下错误:

Java.Lang.IllegalArgumentException: The key must be an application-specific resource id.

Control.Id 为 157

那么为什么我会收到这个错误,我该如何解决呢?

谢谢

【问题讨论】:

    标签: android image xamarin.forms


    【解决方案1】:

    根据SetTag documentation,您不能将任意值设置为键,但应定义应用程序资源ID

    指定的key应该是在应用程序的资源中声明的一个id,以确保它是唯一的(见ID资源类型)。标识为属于 Android 框架或未与任何包关联的键将导致抛出 IllegalArgumentException。

    key与你的控件无关,而是指控件内的标签

    参数

    key:标识标签的key

    tag:一个用来标记视图的对象

    因此,传递您的控件 ID 似乎并不是一个很好的选择。

    请参阅android reference 了解如何创建资源。

    在 XML 中定义的唯一资源 ID。 [...] res/values/filename.xml。文件名是任意的。

    您可以在此文件中创建一个 ID

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item
            type="id"
            name="id_name" />
    </resources>
    

    type="id" 属性对于 ID 资源是必需的。 id_name 可以更改为您喜欢的任何内容,然后在您的应用程序中引用

    Resource.Id.id_name
    

    Resource 类应该在你的 android 应用的主命名空间中)。

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 2021-07-10
      • 2014-08-07
      • 1970-01-01
      • 2016-12-26
      • 2012-11-26
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      相关资源
      最近更新 更多