首页 > 代码库 > WPF中实现登陆窗口的“记住帐号”功能

WPF中实现登陆窗口的“记住帐号”功能

 

1、在Login.xaml中添加资源:

<XmlDataProvider x:Key="XmlDataProvider" Source="pack://application:,,,/5yue.PMS;Component/Config/SysConfig.xml" XPath="SysConfig"/>

 

 

2、使用ComboBox实现帐号输入:

<ComboBox x:Name="UserName" Width="160" Height="26" FontSize="14" IsEditable="True" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Source={StaticResource XmlDataProvider}, XPath=SavedLoginNames/LoginName}" BorderThickness ="1" SelectedIndex="0"/><CheckBox x:Name="ckSaveName" FontSize="12px" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Margin="0,0,22,0" Padding="2 0 " Foreground="#88052742">记住我</CheckBox>

 

 

3、后台逻辑,在提交验证成功后添加保存帐号的步骤即可:

        /// <summary>        /// 保存账号(异步执行)        /// </summary>        /// <param name="loginName"></param>        private async void SaveLoginName(string loginName)        {            await Task.Delay(1);            if(this.ckSaveName.IsChecked != true) return;            try            {                                string filePath = Utils.GetMapPath("")+@"Config\SysConfig.xml";                                XmlDocument doc = XmlHelper.GetXMLDoc(filePath);                if (doc != null)                {                    XmlNode rootNode = doc.SelectSingleNode("SysConfig");                    List<string> nameList = XmlHelper.GetNodesToList(rootNode, "SavedLoginNames");                    if (nameList != null && !nameList.Contains(loginName))// 如果账号仍未保存                    {                        XmlNode namesNode = rootNode.SelectSingleNode("SavedLoginNames");// 获取SavedLoginNames节点信息                        XmlNode childNode = doc.CreateElement("LoginName");// 创建新的SavedLoginName节点                        childNode.InnerText = loginName;// 节点内容值为loginName参数                        namesNode.PrependChild(childNode);// 插入到最前                        int maxCount = Utils.StrToInt(CacheProvider.GetSysConfig("SavedLoginNameCount"), 5);                        namesNode = XmlHelper.HandleNodeUnderMaxCount(namesNode, maxCount);// 处理保存账号的最大数量大于config表中配置的数量                        doc.Save(filePath);// 保存                    }                    else if (nameList != null && nameList.Contains(loginName))// 如果账号已保存, 将其提到最前。                    {                        if (nameList.Count == 1) return;                        XmlNode namesNode = rootNode.SelectSingleNode("SavedLoginNames");                        XmlNodeList childNodes = namesNode.ChildNodes;                        foreach (XmlNode item in childNodes)                        {                            if (loginName.Equals(item.InnerText))                            {                                XmlNode newNode = item;                                namesNode.RemoveChild(item);                                namesNode.PrependChild(newNode);                                break;                            }                        }                        doc.Save(filePath);// 保存                    }                    //else if(nameList == null) return "failed,读取配置失败!";                }                //return (int)Message.SUCCESS;            }            catch (Exception ex)            {                //return ex.ToString();            }        }

 

4、项目根目录/Config下添加SysConfig.xml保存用户名

技术分享
<?xml version="1.0" encoding="UTF-8"?><SysConfig>  <SavedLoginNames><!-- 保存的登录账号 -->    <LoginName>admin</LoginName>    <LoginName>111</LoginName>    <LoginName>555</LoginName>    <LoginName>22</LoginName>    <LoginName>444</LoginName>  </SavedLoginNames></SysConfig>
View Code

 

效果如图:

技术分享

WPF中实现登陆窗口的“记住帐号”功能