Hello, so I'm working on an an app that should basically display a picture after you tap it. Like a photo viewing app.
The problem I have is that when I press one of the images in a grid, the app does what it should, but when I return from the tapGestureRecognizer method, the Image preview in the grid is gone. I wanted to post screenshots but apparently I'm too new a user to be allowed something dangerous as that.
I don't know what I'm doing wrong, it's probably something simple but I can't get my head around it.
Thanks for help!
Code (not working in XAML):
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App7
{
public partial class MainPage : ContentPage
{
private Grid firstGrid;
private bool fullScreenOn = false;
private ContentView mainContentView;
public MainPage()
{
InitializeComponent();
// AppHandler test = new AppHandler();
mainContentView = new ContentView();
firstGrid = new Grid();
firstGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
firstGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
firstGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
// making a square 2x3 grid
firstGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
firstGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
// when clicking an image, this ContentView will fill the entire screen
ContentView fullscreenView = new ContentView();
fullscreenView.BackgroundColor = Color.Black;
fullscreenView.HorizontalOptions = LayoutOptions.FillAndExpand;
fullscreenView.VerticalOptions = LayoutOptions.FillAndExpand;
//mainStackLaout.Children.Add(fullscreenView);
//mainStackLaout.LowerChild(fullscreenView);
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (sender, eventArgs) => {
fullScreenOn = true;
Image fullScreenImage = (Image)sender;
fullscreenView.Content = fullScreenImage;
mainContentView.Content = fullscreenView;
};
Image image1 = new Image() { Source = "aniso.jpg" };
image1.GestureRecognizers.Add(tapGestureRecognizer);
Image image2 = new Image() { Source = "band.png" };
Image image3 = new Image() { Source = "basophil.png" };
Image image4 = new Image() { Source = "basophil2.png" };
Image image5 = new Image() { Source = "bild5.jpeg" };
Image image6 = new Image() { Source = "blast.jpg" };
firstGrid.Children.Add(image1, 0, 0);
firstGrid.Children.Add(image2, 1, 0);
firstGrid.Children.Add(image3, 2, 0);
firstGrid.Children.Add(image4, 0, 1);
firstGrid.Children.Add(image5, 1, 1);
firstGrid.Children.Add(image6, 2, 1);
mainContentView.Content = firstGrid;
Content = mainContentView;
}
protected override bool OnBackButtonPressed()
{
if (fullScreenOn)
{
mainContentView.Content = firstGrid;
fullScreenOn = false;
return true;
}
return base.OnBackButtonPressed();
}
}
}