我正在构建一个Xamarin.Forms PCL应用程序。
我有一些登录逻辑来登录..登录..
基本上:如果存在authtoken,它将直接调用DoLogin()并重定向到我的MasterDetail页面。
代码:
// tries to get remote token and calls DoLogin(Token)
private async void AttemptLogin(string username, string password)
{
Debug.WriteLine("AttemptLogin");
var service = new WebService();
var loginTask = service.AuthorizeCredentials(username, password);
var token = await loginTask;
if (token.Length > 0)
{
DoLogin(token);
}
}
// Tries to get local token and calls DoLogin(Token)
private void TryGetLocalAuthtoken()
{
if (Application.Current.Properties.ContainsKey("authToken"))
{
var authToken = Application.Current.Properties["authToken"].ToString();
DoLogin(authToken);
}
}
private void DoLogin(string token = null)
{
Application.Current.Properties["authToken"] = token; //todo add timestamp
Debug.WriteLine("DoLogin - Token: {0}", token);
Application.Current.MainPage = new CustomerMdPage();
}
正在运行所有这些代码,它启动我的MasterDetail页面并获取数据(来自构造函数中的方法)
但该页面未被重定向到MasterDetail页面,它仍保留在登录页面上。
然后我可以输入凭据和登录,这会将我重定向到我的MasterDetail。
问题:
为什么我第一次运行代码时没有被重定向到我的MasterDetail?
如果这还不够,我会很乐意发布更多代码或澄清。
编辑:请求了MasterDetail代码
public CustomerMdPage()
{
_service = new WebService();
_locator = CrossGeolocator.Current;
_locator.DesiredAccuracy = 50;
_dataAccess = new DataAccess();
var listview = new ListView { ItemTemplate = new DataTemplate(typeof(CustomerViewCell)) };
listview.ItemSelected += (s, e) =>
{
if (e.SelectedItem != null)
{
Detail.BindingContext = e.SelectedItem;
IsPresented = false;
listview.SelectedItem = null;
}
};
Master = new ContentPage()
{
Padding = new Thickness(20, Device.OnPlatform(40, 20, 0), 10, 20),
Title = "Customers",
Content = listview
};
GetCustomers(listview);
Detail = new CustomerDetailPage();
}
答案 0 :(得分:0)
你可以这样做:
在App.cs中,您可以选择应用程序的MainPage,例如:
private bool TryGetLocalAuthtoken()
{
//maybe you can retrieve this with a setting or from the local database.
return Application.Current.Properties.ContainsKey("authToken"));
}
NavigationPage navigationPage = null;
if (TryGetLocalAuthtoken())
{
navigationPage = new NavigationPage(new LoginPage());
}
else
{
navigationPage = new NavigationPage(new MainPage(StaticData.Zero));
}
AppNavigation = navigationPage.Navigation;
MainPage = navigationPage;