The "Remember Account" feature in WPF that implements the login window

Source: Internet
Author: User
Tags xpath

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.