• 如何使用命令模式去调用web服务


    //------------------------------------------------------------程序如何实现一个命令模型------------------------------------------------

    开始的准备:

    [code language="c#"]
          private System.ComponentModel.BackgroundWorker worker;

    [/code]

    1.第一步。也是非常常见的 实现btnOK_Click的功能(也就是点击登陆的功能)
                    private void btnOK_Click(object sender, EventArgs e)
                    {
                            this.Login();
                    }
    2.第二步。实现Login()函数
                     private void Login()
                    {
                            string[] credential = new string[] { txtUserName.Text, txtPassword.Text };

                            lblProgress.Visible = true;
                            lblProgress.Refresh();

                            loginProgress.Visible = true;
                            loginProgress.Value = loginProgress.Minimum;

                            worker.RunWorkerAsync(credential);   //这里采用BackGroundWork,去了解这个控件,到这一步的时候,它会去执行DoWork这个操作
                    }
    3。第三步:  实现DoWork函数
                    private void worker_DoWork(object sender, DoWorkEventArgs e)
                    {
                            // This method will run on a thread other than the UI thread.
                            // Be sure not to manipulate any Windows Forms controls created
                            // on the UI thread from this method.

                            string[] credential = (string[])e.Argument;
                            string userName = credential[0];
                            string password = credential[1];

                            this.Authenticate(userName, password); //这里我们调用验证函数
                 }
    4.第四步:  //在改使用类下新建函数来进行使用该验证
                    private void Authenticate(string userName, string password)
                    {
                            AuthenticateCommand command = new AuthenticateCommand(userName, password);  //注意这里,这里我们已经去调用命令模式里面的东西了
                            ((ICommand)command).Execute(); //执行操作

                            worker.ReportProgress(1, "Loading...");
                    }
    5.第 五步: //这是 命令模式里面的东西。。。。
                    public class AuthenticateCommand : ICommand       
                            {
                    public AuthenticateCommand( string userName, string password )
                                    {
                                            this._UserName = userName;
                                            this._Password = password;
                                    }

                                    void ICommand.Execute()                            //this is user for Execute The Application
                                    {
                                            UserInfo user = _Application.Settings.UserInfo;   //这个是在ObjectModel里面建立的模型
                                            user.UserName = this._UserName;
                                            string passwordHash = HashStringMD5(this._Password);
                                            user.UserPassword = passwordHash;

                                            using (SecurityService service = ServiceProxyFactory.GetSecurityService())
                                            {
                                    user.SecurityKey = service.AuthenticateUser();    //这里才是关键,归根到底是调用WebService的
                   
                                    if (null != user.SecurityKey && user.SecurityKey.Length > 0)
                                    {
                                        this._IsSuccess = true;
                                    }


                                            }
                                    }
                           }
    //------------------------------------------------------------------------------------------the eth-----------------------------------------------

  • 相关阅读:
    10.23
    四则运算(1)
    10.12
    10.22
    今日总结
    Java自学
    Java自学
    Java自学
    Java自学
    Java自学
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/1442207.html
Copyright © 2020-2023  润新知