Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Xamarin Blueprints

You're reading from   Xamarin Blueprints Leverage the power of Xamarin to create stunning cross-platform and native apps

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781785887444
Length 516 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Michael Williams Michael Williams
Author Profile Icon Michael Williams
Michael Williams
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Building a Gallery Application FREE CHAPTER 2. Building a SpeechTalk Application 3. Building a GPS Locator Application 4. Building an Audio Player Application 5. Building a Stocklist Application 6. Building a Chat Application 7. Building a File Storage Application 8. Building a Camera Application

Adding the iOS photo screen

Now that we have our list page, we want to add another UIViewController for displaying selected photos. Let's add a new UIViewController and call it PhotoController. In PhotoController, we are going to build a screen that simply displays the same content in the PhotoCell, but a bit larger.

First, let's add the navigation flow from MainController to PhotoController. We are going to be pushing a new PhotoController whenever a row is selected. Open up TableSource.cs and add the following; at the top, we need to add an EventHandler:

public event EventHandler<GalleryItem> 
 ItemSelected; 

Whenever the row is selected we want to fire this event:

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 
        { 
            if (ItemSelected != null) 
            { 
                ItemSelected (this, galleryItems[indexPath.Row]); 
            } 
 
            tableView.DeselectRow (indexPath, true); 
        } 

Whenever the row is selected, we want to fire this event and pass the gallery item for the index path row. Now we need to handle this event in the MainController class to push a new PhotoController on the navigation stack, but before we do this we need to implement PhotoController:

public partial class PhotoController : UIViewController 
    { 
        /// <summary> 
        /// The image view. 
        /// </summary> 
        private UIImageView _imageView; 
 
        /// <summary> 
        /// The title label. 
        /// </summary> 
        private UILabel _titleLabel; 
 
        /// <summary> 
        /// The date label. 
        /// </summary> 
        private UILabel _dateLabel; 
 
        /// <summary> 
        /// Initializes a new instance of the <see cref="Gallery.iOS.PhotoController"/> class. 
        /// </summary> 
        public PhotoController (ALAsset asset) : base ("PhotoController", null) 
        { 
            _imageView = new UIImageView() 
            { 
                TranslatesAutoresizingMaskIntoConstraints = false, 
                ContentMode = UIViewContentMode.ScaleAspectFit 
            }; 
 
            _titleLabel = new UILabel ()  
            { 
                TranslatesAutoresizingMaskIntoConstraints = false, 
            }; 
 
            _dateLabel = new UILabel ()  
            { 
                TranslatesAutoresizingMaskIntoConstraints = false, 
            }; 
 
            _imageView.Image = new UIImage(asset.DefaultRepresentation.GetFullScreenImage ()); 
            _titleLabel.Text = asset.DefaultRepresentation.Filename; 
            _dateLabel.Text = asset.Date.ToString(); 
        } 

This is very similar to our GalleryCell presentation, but this controller will stack the elements vertically and force the image to scale to fit, keeping the image's correct ratio to avoid any warping. Now let's add ViewDidLoad to lay out the views:

public override void ViewDidLoad () 
        { 
            base.ViewDidLoad (); 
 
            View.Add (_imageView); 
            View.Add (_titleLabel); 
            View.Add (_dateLabel); 
 
            // set layout constraints for main view 
            View.AddConstraints (NSLayoutConstraint.FromVisualFormat("V:|[imageView]-10-[titleLabel(50)]-10-[dateLabel(50)]|", NSLayoutFormatOptions.DirectionLeftToRight, null, new NSDictionary("imageView", imageView, "titleLabel", titleLabel, "dateLabel", dateLabel))); 
 
            View.AddConstraints (NSLayoutConstraint.FromVisualFormat("H:|[imageView]|", NSLayoutFormatOptions.AlignAllTop, null, new NSDictionary ("imageView", imageView))); 
            View.AddConstraints (NSLayoutConstraint.FromVisualFormat("H:|[titleLabel]|", NSLayoutFormatOptions.AlignAllTop, null, new NSDictionary ("titleLabel", titleLabel))); 
            View.AddConstraints (NSLayoutConstraint.FromVisualFormat("H:|[dateLabel]|", NSLayoutFormatOptions.AlignAllTop, null, new NSDictionary ("dateLabel", dateLabel))); 
        } 

There's nothing new here; we are simply adding the three elements and setting our layout constraints accordingly. We stretch all elements to the entire width of the view and stack elements down the pages with the image view on top and a dynamic size based upon the aspect size of the image.

Finally, the last step is to add the event handler whenever a row is selected. We use ImageHandler to fetch ALAsset by the title (filename) in the gallery item, then pass this into the constructor of a new PhotoController and update the constructor of MainController:

        public MainController () : base ("MainController", null) 
        { 
            _source = new TableSource (); 
 
            _source.ItemSelected += (sender, e) =>  
            { 
                var asset = _imageHandler.SynchronousGetAsset (e.Title); 
                NavigationController.PushViewController (new PhotoController (asset), true); 
            }; 
 
            _imageHandler = new ImageHandler (); 
            _imageHandler.AssetsLoaded += handleAssetsLoaded; 
        } 

Excellent! Now run the application and try selecting a few items in the list; you will be navigated to a new PhotoController which will display the selected ALAsset image with its filename and date information.

You have been reading a chapter from
Xamarin Blueprints
Published in: Sep 2016
Publisher: Packt
ISBN-13: 9781785887444
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image