1. Add Resources in Login.xaml:
<x:key= "XmlDataProvider" Source= "pack://application:,,,/5yue. PMS; Component/config/sysconfig.xml " XPath=" SysConfig "/>
2, use the ComboBox to implement account input:
<ComboBoxx:name= "UserName"Width= " the"Height= "+"FontSize= "+"iseditable= "True"Grid.Row= "1"Grid.column= "1"ItemsSource="{Binding Source={staticresource XmlDataProvider}, Xpath=savedloginnames/loginname}"borderthickness= "1"SelectedIndex= "0"/><CheckBoxx:name= "Cksavename"FontSize= "12px"Grid.Row= "1"Grid.column= "2"HorizontalAlignment= "Right"Margin= "0,0,22,0"Padding= "2 0"Foreground= "#88052742">Remember Me</CheckBox>
3, the background logic, after the submission of verification success, add the steps to save the account:
/// <summary> ///Save account (asynchronous execution)/// </summary> /// <param name= "LoginName" ></param> Private Async voidSaveloginname (stringloginName) { awaitTask.delay (1); if( This. cksavename.ischecked! =true)return; Try { stringFilePath = 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))//if the account is still not saved{XmlNode Namesnode= Rootnode.selectsinglenode ("Savedloginnames");//Get savedloginnames node informationXmlNode Childnode = doc. CreateElement ("LoginName");//Create a new savedloginname nodeChildnode.innertext = LoginName;//node content value is loginname parameterNamesnode.prependchild (Childnode);//insert to the front intMaxCount = Utils.strtoint (Cacheprovider.getsysconfig ("Savedloginnamecount"),5); Namesnode= Xmlhelper.handlenodeundermaxcount (Namesnode, MaxCount);//the maximum number of processing saved accounts is greater than the number configured in the Config tableDoc. Save (FilePath);//Save } Else if(NameList! =NULL&& namelist.contains (LoginName))//If the account has been saved, refer it to the front. { if(Namelist.count = =1)return; XmlNode Namesnode= Rootnode.selectsinglenode ("Savedloginnames"); XmlNodeList ChildNodes=Namesnode.childnodes; foreach(XmlNode IteminchchildNodes) { if(Loginname.equals (item. InnerText)) {XmlNode NewNode=item; Namesnode.removechild (item); Namesnode.prependchild (NewNode); Break; }} doc. Save (FilePath);//Save } //else if (namelist = = null) return "failed, read configuration failed! "; } //return (int) message.success; } Catch(Exception ex) {//return ex. ToString (); } }
4, the project root directory/config add sysconfig.xml Save user name
<?XML version= "1.0" encoding= "UTF-8"?><SysConfig> <Savedloginnames><!--saved log-in account - <LoginName>Admin</LoginName> <LoginName>111</LoginName> <LoginName>555</LoginName> <LoginName>22</LoginName> <LoginName>444</LoginName> </Savedloginnames></SysConfig>
View Code
Effect
The "Remember Account" feature in WPF that implements the login window