【问题标题】:Remember me checkbox functionality in windows phone 8Windows Phone 8 中的记住我复选框功能
【发布时间】:2014-05-23 16:25:51
【问题描述】:

如何添加“记住我”登录功能?

代码:

<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 复选框时,它应该保存名称和密码的值。

给我建议。

【问题讨论】:

  • 更具体。您在哪一部分特别有问题?
  • 您可以尝试使用IsolatedStorageSettings在应用重启过程中保持简单的键值对(应用关闭并重新打开后该值仍然存在)。
  • @har07 我想做这样的(见链接):aspsnippets.com/demos/276/Default.aspx 它,可以用独立存储吗?
  • 是的,可以使用独立存储

标签: windows-phone-8 checkbox


【解决方案1】:

回复您的评论,似乎逻辑有点不对劲。无论用户上次登录是否选中,您的复选框都将处于默认状态。试试这种方式:

//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); 
    }
}

【讨论】:

  • @har07 代码无法正常工作。给出如下错误:mscorlib.ni.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理。我的代码在上面。
  • 感谢您的宝贵时间。
【解决方案2】:

希望下面的代码有所帮助。 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】:
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);
        }

    }
  }
}

【讨论】:

    【解决方案4】:

    公共登录页面()

    {

    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("名称", txtName.Text); }

                if ((userSettings.Contains("Pass")) == false)
                {
                    userSettings.Add("Password", txtPassword.Password);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    

    }

    【讨论】:

    • @Baradi:它给出的错误如下:已添加具有相同密钥的项目。
    • 这里的用户设置是什么?
    • 好的,在构造函数中您正在检查 userSettings.Contains("Name"),但在按钮点击中您正在检查 (userSettings.Contains("UserName"))存在则 userSettings.Add("Name", txtName.Text);每次执行。因此,如果它第一次执行,它不会抛出错误,但是第二次执行时,键“Name”已经存在,并且您正在第二次添加它。所以它会抛出 key already exists 错误。
    • @Baradi:帮我解决这个问题。
    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2017-11-15
    • 1970-01-01
    相关资源
    最近更新 更多