- Xamarin Blueprints
- Michael Williams
- 551字
- 2021-07-08 11:48:20
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.
- Facebook Application Development with Graph API Cookbook
- Java技術手冊(原書第7版)
- Java深入解析:透析Java本質的36個話題
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- C語言程序設計
- 低代碼平臺開發實踐:基于React
- Learning Docker Networking
- 計算機應用基礎項目化教程
- Mastering Unreal Engine 4.X
- Developing Java Applications with Spring and Spring Boot
- Java面向對象程序設計教程
- 數據結構與算法詳解
- 趣學數據結構
- Learning Java by Building Android Games
- Building Business Websites with Squarespace 7