Reputation: 111
In my xamarin.forms Project i got an Image. This Image has an Renderer which Looks like this:
public class CustomImage : Image
{
public event EventHandler<PointEventArgs> TapEvent;
public void OnTapEvent(int x, int y)
{
TapEvent?.Invoke(this, new PointEventArgs(x, y));
}
}
The Android Renderer Looks like this:
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 });
}
}
}
}
When I call the method Control.setTag i get the following error:
Java.Lang.IllegalArgumentException: The key must be an application-specific resource id.
Control.Id is 157
So why im getting this error and how can i fix it?
Thanks
Upvotes: 0
Views: 419
Reputation: 9742
According to the SetTag documentation you can't set any arbitrary value as the key, but should define an application resource id
The specified key should be an id declared in the resources of the application to ensure it is unique (see the ID resource type). Keys identified as belonging to the Android framework or not associated with any package will cause an IllegalArgumentException to be thrown.
The key is not related to your control, but refers to the tag within the control
Parameters
key: The key identifying the tag
tag: An Object to tag the view with
Hence passing your controls ID does not seem like a very good choice anyway.
Please see the android reference on how to create a resource.
A unique resource ID defined in XML. [...]
res/values/filename.xml
. The filename is arbitrary.
Within this file you can create an ID
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="id_name" />
</resources>
The attribute type="id"
is mandatory for ID resources. id_name
can be changed to whatever you like and then referenced in your application
Resource.Id.id_name
(Resource
class should be in the main namespace of your android app).
Upvotes: 1