【问题标题】:How to close the current window in Xamarin forms?如何关闭 Xamarin 表单中的当前窗口?
【发布时间】:2016-12-05 10:52:34
【问题描述】:

我有一个 Xamarin Forms PCL 项目,它由几个选项卡式页面组成。当应用程序启动时,它会检查用户是否已登录。如果未登录,则加载登录屏幕,否则加载 MainPage(创建选项卡式项目)。

我遇到的问题是,在登录屏幕上,我只想在他们成功登录后关闭此窗口,但是通过使用 PopModalAsync,页面无法获取自身的实例(导致空异常) .然后该页面就坐在那里什么也不做,直到您重新启动它并让您进入。

我需要一种关闭登录屏幕的方法,但似乎找不到关于如何做到这一点的明确建议。

App.cs:

private async void Login_Clicked(object sender, EventArgs e)
{
    if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
    {
        indicator.IsVisible = true;
        indicator.IsRunning = true;
        LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);

        try
        {
            if (liuir.user != null)
            {
                if (liuir.user.userId > 0)
                {
                    UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
                    await WebDataAccess.SaveSwipesToCloud();
                    await Navigation.PushModalAsync(new MainPage());
                    //var poppedPage = await Navigation.PopModalAsync();
                }
                else
                {
                    DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                    indicator.IsVisible = false;
                    indicator.IsRunning = false;
                }
            }
            else
            {
                DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }

        }
        catch (Exception ex)
        {
            ErrorRepository.InsertError(ex.ToString());
        }
    }
    else
    {
        DisplayAlert("Login", "You must enter both a username and password", "Ok");
        indicator.IsVisible = false;
        indicator.IsRunning = false;
    }
}

登录界面:

private async void Login_Clicked(object sender, EventArgs e)
{
    if ((!string.IsNullOrWhiteSpace(username.Text) && !string.IsNullOrWhiteSpace(password.Text)))
    {
        indicator.IsVisible = true;
        indicator.IsRunning = true;
        LoggedInUserInfoRoot liuir = await WebDataAccess.LoginUser(username.Text, password.Text);

        try
        {
            if (liuir.user != null)
            {
                if (liuir.user.userId > 0)
                {
                    UserInfoRepository.UpdateUserInformation(liuir.user.userId, DateTime.Now, liuir.user.userName);
                    await WebDataAccess.SaveSwipesToCloud();
                    var poppedPage = await Navigation.PopModalAsync();
                }
                else
                {
                    DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                    indicator.IsVisible = false;
                    indicator.IsRunning = false;
                }
            }
            else
            {
                DisplayAlert("Login", "Your username or password is incorrect", "Ok");
                indicator.IsVisible = false;
                indicator.IsRunning = false;
            }

        }
        catch (Exception ex)
        {
            ErrorRepository.InsertError(ex.ToString());
        }
    }
    else
    {
        DisplayAlert("Login", "You must enter both a username and password", "Ok");
        indicator.IsVisible = false;
        indicator.IsRunning = false;
    }
}

主页:

public MainPage()
{
    Children.Add(new Clocking());
    Children.Add(new ChangePassword{ BackgroundColor = Color.FromHex("59a092") });
    Children.Add(new CompanySetup { BackgroundColor = Color.FromHex("59a092") });
    Children.Add(new Log { BackgroundColor = Color.FromHex("59a092") });
    isuserloggedin();
}

public async void isuserloggedin()
{
    bool isuserloggedin = false;
    if (UserInfoRepository.UserLoggedInTime() != null)
    {
        if (UserInfoRepository.UserLoggedInTime() < DateTime.Now.AddMinutes(-60))
        {
            isuserloggedin = false;
        }
        else
        {
            isuserloggedin = true;
        }
    }
    if (isuserloggedin == false)
    {
        //  MainPage = new Login { BackgroundColor = Color.FromHex("59a092") };
        await Navigation.PushModalAsync(new Login());
    }

}

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我的标签页

    public class MyTabbedPage:TabbedPage
    

    别忘了用这个推送你的页面

    await Navigation.PushModalAsync(new NavigationPage(new MyTabbedPage()));
    

    我有比你的代码更多的是这个

    BackgroundColor = Color.FromRgb (76, 108, 139)
    Device.OnPlatform(() => 
    {
        this.Padding = new Thickness(0, 30, 0, 0);
    });
    
    and i add my pages with this function
    void AddPage(string title, params View[] views) 
        {
            var content = new StackLayout();
            foreach (var view in views)
                content.Children.Add(view);
            this.Children.Add(new ContentPage 
            {
                Content = content,
                Title = title
            });
        }
    

    希望对你有帮助

    【讨论】:

    • 这样我得到一个错误:System.MissingMethodException: Method 'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener' not found.
    • 怎么来..我有一个标签页,我用这段代码来做..它工作..你如何创建你的标签页?
    • 我添加了 MainPage 代码,显示了如何创建标签页。
    • 我认为您的问题不在于创建标签页。查看这篇文章..missingmethod 我们正在牺牲时间来帮助人们......
    • 问题是页面包含内容。如果我将它们更改为继承 TabbedPage,它会在很多对象(如“DisplayAlert”和“Navigation”)处引发错误(该名称在当前上下文中不存在)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多