Xamarin android--Data binding to Controls (ii)

Source: Internet
Author: User

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 views
if(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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.