简体   繁体   中英

Navigation- PopAsync() does not seem to work

I am creating a login form using Xamarin which accepts email, password fields.

After the user enters his details and clicks on login button, no alert box is displayed. It comes out of the app. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;

using Xamarin.Forms;
using SQLite;
using System.IO;

namespace LoginPage.Views
{
 public class AddDetails : ContentPage
 {
    private Entry _emailEntry;
    private Entry _passwordEntry;
    private Button _saveButton;

    string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");
    public AddDetails()
    {
        this.Title = "Add Details";
        StackLayout stackLayout = new StackLayout();



        _emailEntry = new Entry();
        _emailEntry.Keyboard = Keyboard.Text;
        _emailEntry.Placeholder = "Email";
        stackLayout.Children.Add(_emailEntry);


        _passwordEntry = new Entry();
        _passwordEntry.Keyboard = Keyboard.Text;
        _passwordEntry.Placeholder = "Password";
        stackLayout.Children.Add(_passwordEntry);

        _saveButton = new Button();
        _saveButton.Text = "Login User";
        _saveButton.Clicked += _saveButton_Clicked;
        stackLayout.Children.Add(_saveButton);

        Content = stackLayout;
    }

    private async void _saveButton_Clicked(object Sender, EventArgs e)
    {
        var db = new SQLiteConnection(_dbPath);
        db.CreateTable<User>();

        var maxPk = db.Table<User>().OrderByDescending(c => c.Id).FirstOrDefault();

        User user = new User()
        {
            Id = (maxPk == null ? 1 : Convert.ToInt32(maxPk.Id) +1),
            Email = _emailEntry.Text,
            Password = _passwordEntry.Text

        };

        db.Insert(user);
        await DisplayAlert(null, user.Email + "Saved", "Ok");
        await Navigation.PopAsync();
     }

     }
     }

Login screen gets displayed, but no alert box which should say Email saved. Also, it comes out of the app abruptly

Have you set MainPage = new NavigationPage (new Page1Xaml ());

Please follow this link https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM