2012-12-07 16 views
8

Boş bir özel düzenden bir alt düzenden bir çocuk düzenine geçmeye çalışılıyor.monodroid/xamarin Özel öznitelikleri, ObtainStyledAttributes

ObtainStyledAttributes() öğesinden döndürülen TypedArray, kimlikleri Resource.designer'daki değerlerle eşleyebilmeme rağmen, oluşturduğum özel özellikler için karşılık gelen özel değerlere sahip görünmüyor.


Attr.xml:

<resources> 
<declare-styleable name="HeaderView"> 
    <attr name="bgcolor" format="color" /> 
    <attr name="testing" format="string" /> 
</declare-styleable> 

Main.xaml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <views.HeaderView 
      android:id="@+id/hdrWatchList" 
      android:layout_width="fill_parent" 
      android:layout_height="20.0dp" 
      custom:bgcolor="@color/blue" 
      custom:testing="testing text buddy" /> 

Görünüm Sınıfı:

public HeaderView (Context context, IAttributeSet attrs) : 
     base (context, attrs) 
    { 
     int[] styleAttrs = Resource.Styleable.HeaderView; 
     TypedArray a = context.ObtainStyledAttributes(attrs, styleAttrs); 

     string sid = a.GetString(Resource.Styleable.HeaderView_testing); 
     int id = a.GetColor(Resource.Styleable.HeaderView_bgcolor, 555); 

     Log.Info("testing", "resource sid : " + sid); // RETURNS '' 
     Log.Info("testing", "resource id : " + id); // RETURNS DEF 555 

cevap

6

bence sorun sizin xmlns:custom belirtilen nasıl aittir ad. Sen dizenin sonunda uygulamalar ad eklemeniz gerekir senin zaten şöyle var:

xmlns:custom="http://schemas.android.com/apk/res/my.awesome.namespace" 

Ayrıca AndroidManifest.xml aynı ad tanımladığınız Android projesi için tanımlanmış olması gerekir. Ayrıca

hatları:

int[] styleAttrs = Resource.Styleable.HeaderView; 
TypedArray a = context.ObtainStyledAttributes(attrs, styleAttrs); 

bana biraz garip bak ve ben sadece yazardım:

var a = context.ObtainStyledAttributes(attrs, Resource.Styleable.HeaderView); 

daha sonra styleAttrs kullanmıyorsanız Hele.

DÜZENLEME: Android SDK beri kullanmak mümkündür 17 rev:

xmlns:custom="http://schemas.android.com/apk/res-auto" 

tamamı yerine ad yazmak zorunda.