This example shows a picture in gallery that gets a native from a media content provider.
Simple initialization code in the activity
Private void initgallery () { = findviewbyid<gallery> (Resource.Id.gallery); New Imageadapter (this); }
The Imageadapter class defines the adapter for itself, inheriting the Baseadapter class, where the core code is the GetView method.
Baseadapter must implement the specified method for the abstract class, right-Baseadapter the class, select Implement abstract class, and add the method to be implemented.
Code implementation of Imageadapter
Public classImageadapter:baseadapter {Privatecontext Context; PublicImageadapter (Context context) { This. Context =context; } #regionImplemented abstract members of Baseadapter Public OverrideJava.Lang.Object GetItem (intposition) {imagecursor.movetoposition (position); returnImagecursor.getstring (0); } Public Override LongGetitemid (intposition) { returnposition; } Public OverrideView GetView (intposition, View Convertview, ViewGroup parent) {
Avoid constant destruction and creation of new viewsif(Convertview = =NULL) {ImageView ImageView=NewImageView (context); Imagecursor.movetoposition (position); varIMAGEURL = imagecursor.getstring (0); Console.WriteLine (IMAGEURL);
Determine MediaStore.Images.Media.InterfaceConst.Data query result as Picture path according to output//Imageview.setimageuri (Android.Net.Uri.WithAppendedPath (Mediastore.images.media.externalcontenturi,imageid) );Imageview.setimageuri (Android.Net.Uri.Parse (IMAGEURL)); Imageview.setscaletype (ImageView.ScaleType.FitCenter); returnImageView; } Else { returnConvertview asImageView; } } Public Override intCount {Get { returnImagecursor.count; } } #endregion PrivateAndroid.Database.ICursor Imagecursor; PublicAndroid.Database.ICursor Imagecursor {Get{ if(imagecursor==NULL) {Imagecursor=getimagecursor (); } returnImagecursor; } Set{imagecursor =value;} } //Query Native Pictures PrivateAndroid.Database.ICursor getimagecursor () {return(Context asActivity). Managedquery (MediaStore.Images.Media.ExternalContentUri,New string[] {MediaStore.Images.Media.InterfaceConsts.Data},NULL,NULL,NULL); } }
Note: It is important to ensure that there is a picture file in the machine or simulator, otherwise the exception of no entry for content will be reported.
In addition to learn the process of learning a Xamarin component, address: Http://components.xamarin.com/view/xamarin.mobile, do not know how to effect.