在尝试设置MainPage时,对象引用未设置为对象的实例

时间:2017-08-29 11:06:25

标签: xamarin xamarin.android xamarin.forms

我试图在注销按钮点击时设置MainPage,但我得到的是下面提到的异常。

退出功能

private void Logout()
    {
        //Settings.Current.Remove(Constants.PREF_IS_LOGGED_IN);
        //Settings.Current.Remove(Constants.PREF_AUTH_TOKEN);
        MainPage = new NavigationPage(new LoginPage());
    }

LoginPageViewModel:

using Prism.Mvvm;
using Prism.Navigation;
using SalesApp.Controllers.RestController;
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Acr.Settings;
using Plugin.Connectivity;
using SalesApp.Utils;
using SalesApp.Views;
using SalesApp.Views.SideMenu;
using Acr.UserDialogs;
using SalesApp.Interfaces;

namespace SalesApp.ViewModels
{
    public class LoginPageViewModel : BindableBase, INavigationAware
    {
        private String _email = Constants.Username; // dummy value
        private String _password = Constants.Password; // dummy value
        private Boolean _isShowPassword = true;
        private Boolean _isShowPasswordIcon = false;
        private Boolean _isRememberMe = false;

        ICommand _tapCommandShowPassword;
        ICommand _tapCommandNavigateForgotPassword;
        ICommand _login;
        INavigationService _navigationService;

        public DataManager DataManager { get; private set; }

        public LoginPageViewModel(INavigationService navigationService)
        {
            _tapCommandShowPassword = new Command(ShowHidePassword);
            _tapCommandNavigateForgotPassword = new Command(NavigateToForgotPassword);
            _login = new Command(LoginApiCalls);
            _navigationService = navigationService;
        }
        public String Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
            }
        }
        public String Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
                if (_password.Length > 0)
                    IsShowPasswordIcon = true;
                else
                {
                    IsShowPasswordIcon = false;
                    IsShowPassword = true;
                }
            }
        }
        public bool IsShowPassword
        {
            get
            {
                return _isShowPassword;
            }
            set
            {
                _isShowPassword = value;
                RaisePropertyChanged("IsShowPassword");
            }
        }

        public bool IsRememberMe
        {
            get
            {
                return _isRememberMe;
            }
            set
            {
                _isRememberMe = value;
                RaisePropertyChanged("IsRememberMe");
            }
        }

        public bool IsShowPasswordIcon
        {
            get
            {
                return _isShowPasswordIcon;
            }
            set
            {
                _isShowPasswordIcon = value;
                RaisePropertyChanged("IsShowPasswordIcon");
            }
        }

        public ICommand TapCommandShowPassword
        {
            get { return _tapCommandShowPassword; }
        }
        public ICommand TapCommandNavigateForgotPassword
        {
            get { return _tapCommandNavigateForgotPassword; }
        }
        public ICommand Login
        {
            get { return _login; }
        }

        public void OnNavigatedFrom(NavigationParameters parameters)
        {

        }

        public void OnNavigatedTo(NavigationParameters parameters)
        {

        }

        public void OnNavigatingTo(NavigationParameters parameters)
        {

        }

        void ShowHidePassword()
        {
            IsShowPassword = !IsShowPassword;
            if (IsShowPassword)
                IsShowPassword = true;
            else
                IsShowPassword = false;

        }

        void NavigateToForgotPassword()
        {
            _navigationService.NavigateAsync("ForgotPasswordPage");

        }

        async void LoginApiCalls(object s)
        {

            if (CrossConnectivity.Current.IsConnected)
            {

                try
                {
                    UserDialogs.Instance.ShowLoading(ResX.AppResources.Loading_mesg, MaskType.Black);
                    if (_tapCommandNavigateForgotPassword != null)
                    {
                        DataManager = new DataManager(new RestService());

                        var resp = await DataManager.GetAuthToken(_password);

                        var isSuccess = false;
                        if (resp != null)
                            isSuccess = resp.IsSuccess;

                        if (isSuccess)
                            resp = await DataManager.GetUerAuth(_email, resp.Data);

                        if (resp.IsSuccess)
                        {
                            //store flag in shared pref
                            if (IsRememberMe)
                            {
                                Settings.Current.Set(Constants.PREF_IS_LOGGED_IN, true);
                            }

                            Settings.Current.Set(Constants.PREF_SALES_AGENT_ID, resp.SalesAgentId);

                            UserDialogs.Instance.HideLoading();
                            App.Current.MainPage = new NavigationPage(new HomePage());
                            //_navigationService.NavigateAsync("/HomePage");
                        }
                        else
                        {
                            await App.Current.MainPage.DisplayAlert(ResX.AppResources.Error_txt,
                                ResX.AppResources.UserName_Password_Error,ResX.AppResources.OK_txt);
                            UserDialogs.Instance.HideLoading();
                        }
                    }
                }
                catch (Exception e)
                {
                    await App.Current.MainPage.DisplayAlert(ResX.AppResources.Error_txt,
                                ResX.AppResources.Generic_Error_txt, ResX.AppResources.OK_txt);
                    UserDialogs.Instance.HideLoading();
                }
            }
            else
            {
                await App.Current.MainPage.DisplayAlert(ResX.AppResources.Net_Error_txt,
                    ResX.AppResources.No_Network_Error_txt, ResX.AppResources.OK_txt);
            }
        }

        public void OnPhoneTapped(String phone)
        {
            var dialer = DependencyService.Get<IDialer>();
            if (dialer != null)
            {
                dialer.Dial(phone);
            }
        }

        public void OnRouteTap(string sourceLat, string sourceLon, string destinationLat, string destinationLon)
        {
            var route = DependencyService.Get<IDirection>();
            if (route != null)
            {
                route.DrawDirection(sourceLat, sourceLon, destinationLat, destinationLon);
            }
        }
    }
}


    UNHANDLED EXCEPTION:
08-29 15:59:43.868 I/MonoDroid( 3268): System.NullReferenceException: Object reference not set to an instance of an object.
08-29 15:59:43.868 I/MonoDroid( 3268):   at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:292 
08-29 15:59:43.868 I/MonoDroid( 3268):   at Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean p0, System.Int32 p1, System.Int32 p2, System.Int32 p3, System.Int32 p4) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android.FormsViewGroup\obj\Release\generated\src\Xamarin.Forms.Platform.Android.FormsViewGroup.cs:198 
08-29 15:59:43.868 I/MonoDroid( 3268):   at (wrapper dynamic-method) System.Object:bbd5dc5a-77cf-45bd-9d16-717bbe7e4232 (intptr,intptr,bool,int,int,int,int)

0 个答案:

没有答案