如何添加"记住我"登录功能?
代码:
<Grid x:Name="LayoutRoot" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="70,50,70,50" Orientation="Vertical">
<TextBox Name="txtName"/>
<PasswordBox Name="txtPassword"/>
<Button Tap="Button_Tap"/>
</StackPanel>
<StackPanel Margin="70,430">
<CheckBox Name="cbStayIn" Content="StayIn"/>
</StackPanel>
</Grid>
当用户在StayIn复选框中签入时,它应该包含姓名和密码的值。
给我建议。
答案 0 :(得分:0)
回应你的评论,似乎逻辑有点偏。无论用户是否上次登录,您的复选框都将处于默认状态。请尝试以下方式:
//on page load :
InitializeComponent();
//check if remember me checked the last time user login
bool stayin = false;
userSettings.TryGetValue<bool>("stayin", stayin);
//if checked, load username & password from iso store
if (stayin)
{
string Email = (string)userSettings["email"];
txtName.Text = Email;
string Password = (string)userSettings["password"];
txtPassword.Password = Password;
}
......
//on login button clicked
private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//if remember me checked,
//save mail, password, and stayin = true in iso store
if (cbStayIn.IsChecked == true)
{
userSettings.Add("email", txtName.Text);
userSettings.Add("password", txtPassword.Password);
userSettings.Add("stayin", true);
}
}
答案 1 :(得分:0)
using System.IO.IsolatedStorage;
namespace iso
{
public partial class LoginPage : PhoneApplicationPage
{
private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
public LoginPage()
{
InitializeComponent();
bool stayin = false;
// userSettings.TryGetValue<bool>("stayin", stayin);
userSettings.TryGetValue<bool>("stayin", out stayin);
if (stayin)
{
string Email = (string)userSettings["email"];
txtName.Text = Email;
string Password = (string)userSettings["password"];
txtPassword.Password = Password;
}
}
private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (cbStayIn.IsChecked == true)
{
userSettings.Add("email", txtName.Text);// Error :An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code
userSettings.Add("password", txtPassword.Password);
userSettings.Add("stayin", true);
}
}
}
}
答案 2 :(得分:0)
希望下面的代码有帮助。 dint编译代码,原谅编译错误
If(checkbox.Selected == true)
{
//check if the UserName and PassWord are already stored
if (IsolatedStorageSettings.ApplicationSettings.Contains("userName"))
{
UserName.Text = IsolatedStorageSettings.ApplicationSettings["userName"].ToString();
}
else
{
IsolatedStorageSettings.ApplicationSettings["userName"] = UserNameTextBox.Text;
IsolatedStorageSettings.ApplicationSettings.Save();
}
if (IsolatedStorageSettings.ApplicationSettings.Contains("Password"))
{
PasswordBox.Text = IsolatedStorageSettings.ApplicationSettings["Password"].ToString();
}
else
{
IsolatedStorageSettings.ApplicationSettings["Password"] = PasswordBox.Text;
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
答案 3 :(得分:0)
public LoginPage()
{
InitializeComponent();
if (cbStayIn.IsChecked == true)
{
if (userSettings.Contains("Name"))
{
txtName.Text = userSettings["Name"].ToString();
}
if (userSettings.Contains("Password"))
{
txtPassword.Password = userSettings["Password"].ToString();
}
}
}
private void Button_Tap(object sender,System.Windows.Input.GestureEventArgs e)
{ // 尝试 { if((userSettings.Contains(“UserName”))== false) { userSettings.Add(“Name”,txtName.Text); }
if ((userSettings.Contains("Pass")) == false)
{
userSettings.Add("Password", txtPassword.Password);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}