2017-11-19 28 views
0

ログインユーザーとログアウトユーザーに問題があります。すべてが良い動作しますが、ログアウトは初めてで、私はボタンのログインおよびクリックした後をクリックした場合、私は再びアプリを起動した場合、私は唯一の空白のページを持っていると私は最初のケースに似XamarinフォームのユーザーログインとログアウトがAndroidで機能しない

App.csコードを作品thahtアプリをアンインストールする必要があります。

public App() 
{ 
    InitializeComponent(); 

    if (!Current.Properties.ContainsKey("IsLoggedIn")) 
    { 
     Current.Properties["IsLoggedIn"] = false; 
     if ((bool)Current.Properties["IsLoggedIn"] == false) 
     { 
      MainPage = new LoginPage(); 
     } 
     else 
     { 
      MainPage = new NavigationPage(new MainPage()); 
     } 
    } 
} 

ログインページ:

async private void Button_Clicked_Login(object sender, EventArgs e) 
{ 
    Application.Current.Properties["IsLoggedIn"] = true; 
    await Application.Current.SavePropertiesAsync(); 
    Application.Current.MainPage = new NavigationPage(new MainPage()); 
} 

がログアウト:

async private void Button_Clicked(object sender, EventArgs e) 
{ 
    Application.Current.Properties["IsLoggedIn"] = false; 
    await Application.Current.SavePropertiesAsync(); 
    Application.Current.MainPage = new LoginPage(); 
} 

答えて

2

問題がある

if (!Current.Properties.ContainsKey("IsLoggedIn")) 

最初のアプリの起動時に、そのプロパティが存在するかどうかを確認しています。それは存在しないので、if文に入ります。しかし、その後、そのプロパティを割り当て、次回の起動時には常に失敗します。

私はそのような何かに再作成する場合は、その文にあなたを示唆している:

if (!Current.Properties.ContainsKey("IsLoggedIn")) { 
     Current.Properties["IsLoggedIn"] = false; 
     await Application.Current.SavePropertiesAsync(); 
     MainPage = new LoginPage(); 
    } else { 
     if(Current.Properties["IsLoggedIn"] == true) { 
      MainPage = new NavigationPage(new MainPage()); 
     } else { 
      MainPage = new LoginPage(); 
     }   
    } 
関連する問題