引用类型的Xamarin自定义属性不起作用

时间:2016-08-30 20:08:37

标签: android xamarin

我正在尝试为我的控件创建自定义属性。这是我的attrs.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <declare-styleable name="ImageView">
    <attr name="testcustom" format="reference"/>
  </declare-styleable>
</resources>

在我的root测试应用中,我添加了这个命名空间xmlns:TestCustom =&#34; http://schemas.android.com/apk/res-auto"

稍后在我的布局文件中,我有一个图像视图

<ImageView
    android:src="@drawable/Icon"
    TestCustom:testcustom="@drawable/Icon"/>

第一个android:src属性没问题,但TestCustom不起作用。

给出的错误是&#34;找不到与给定名称匹配的资源(在&#39; testcustom&#39;值为&#39; @ drawable / icon)

那么......这里发生了什么?有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您需要为特定的包名称空间指定xmlns:。例如:

xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"

表示custom:的包名称空间的com.example.customviews

您可以在此处详细了解:https://developer.android.com/training/custom-views/create-view.html#customattr

注意:您可能需要考虑遵循camelCase关于自定义属性,视图等的惯例。

答案 1 :(得分:0)

所以回顾一下:这里的解决方案是在通过自定义属性引用资源时使用小写名称。显然Xamarin在后台做了一些工作,通常允许以大写字母开头的资源就可以了,但它可能无法解决这个问题。

如果您使用的是Xamarin和自定义属性,并且无法引用您的资源,请以小写形式试用!!!

或者像Jon建议的那样,不要使用大写的东西来获取资源。