我创建了一个Xamarin.Forms应用,允许用户使用用户名&密码。根据他们的用户名,他们的角色被定义。
我的模型如下:
public class UserDetails
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool Truck { get; set; }
public bool Car { get; set; }
public bool Bus { get; set; }
}
public class TransportDetails
{
public int Id { get; set; }
public string Name { get; set; }
public string Url{ get; set; }
}
现在我创建了一个包含硬编码数据的基本列表,如下所示:
public List<UserDetails> GetAllUserDetails()
{
List<UserDetails> userDetails = new List<UserDetails>
{
new UserDetails
{
Id = 1,
Username = "admin",
Password = "123",
Truck = false,
Car = true,
Bus = true
},
new UserDetails
{
Id = 2,
Username = "superadmin",
Password = "123",
Truck = true,
Car = false,
Bus = true
}
};
return userDetails;
}
我还有一个列表,其中包含有关传输的数据:
public List<TransportDetails> GetAppDetails()
{
List<TransportDetails> transportDetails = new List<TransportDetails>
{
new TransportDetails
{
Id = 1,
Name = "Truck",
UrlAndroid = "http://192.168.11.15/apps/truck"
},
new TransportDetails
{
Id = 2,
Name = "Car",
UrlAndroid = "http://192.168.11.15/apps/car"
},
new TransportDetails
{
Id = 3,
Name = "Bus",
UrlAndroid = "http://192.168.11.15/apps/bus"
};
return transportDetails;
}
现在在我的LoginViewModel
中,我检查登录凭据并导航到另一个页面,如下所示:
public void CheckLogin()
{
Data data = new Data();
allUsers = new List<UserDetails>();
allUsers = data.GetAllUserDetails();
if (allUsers.Any(x => x.Username.Equals(Username) && x.Password.Equals(Password)))
{
Application.Current.MainPage.Navigation.PushAsync(new MainPage(Username));
}
else
{
Application.Current.MainPage.DisplayAlert("Error", "Invalid credentials", "OK");
}
}
我想要实现的是,在检查凭据时,如果凭据是正确的,我想获取用户有权获得的传输详细信息的相应数据。
与用户Admin
一样,他有权获取Car
和Bus
的数据。如何在导航到MainPage
时通过构造函数传递这些数据?
请帮忙。
答案 0 :(得分:2)
而不是仅将其UserName传递给MainPage,传递其UserDetails对象
if (allUsers.Any(x => x.Username.Equals(Username) && x.Password.Equals(Password)))
{
var user = allUsers.Where(x => x.Username.Equals(Username) && x.Password.Equals(Password)).First();
Application.Current.MainPage.Navigation.PushAsync(new MainPage(user));
}