不同于Objective C,在Monotouch中,当建立Alertview个体,并调用show函数显示的时候存在延时,取决于你接下来的代码。所以为了保证同步进行,可以建立一个新的Thread。让新的线程来执行警告框和进度轮的显示,而主线程继续执行,当需要停止的时候只需要调用警告框的DismissWithClickedButtonIndex()函数。
这是一个按钮触发事件,当用户点击按钮,首先先建立一个线程,线程执行的操作是函数ThreadLogin(), 即产生一个警告框以及一个进度轮,关于这两个控件的定义的位置我是在类中直接定义的。
建立好线程--启动线程--然后是我自己写的一个类,不是什么复杂的东西,就是通过HttpResquest来验证下用户的密码是否正确。
当用户密码错误,即httpServie.Authenticate()的返回值为false时,先判断线程是否还在运行状态,如果是,那么关闭。如果已经关闭,那么将警告框和进度轮关闭。并且弹出一个新的警告框提示用户登陆错误。
当用户密码正确,即返回值为true,判断线程是否存在,不存在关闭,如果已经结束,那么将警告框和进度轮关闭。进行下一步你想进行的操作
UIAlertView baseAlert;
UIActivityIndicatorView ActView ;
partial void actLogin (MonoTouch.Foundation.NSObject sender)
{
Thread threadlogin = new Thread(ThreadLogin as ThreadStart);
threadlogin.Start();
HttpService httpServie= new HttpService(textName.Text,textPassWord.Text);
if(!httpServie.Authenticate()){
if(threadlogin!= null && threadlogin.IsAlive)
{
threadlogin.Abort();
threadlogin.Join();
}
baseAlert.DismissWithClickedButtonIndex(0,false);
UIAlertView alertview =new UIAlertView("","Invalide user name or password",null,"OK",null);
alertview.Show();
}
else{
if(threadlogin!= null && threadlogin.IsAlive)
{
threadlogin.Abort();
threadlogin.Join();
}
baseAlert.DismissWithClickedButtonIndex(0,false);
if(this.addItemScreen==null){
this.addItemScreen=new AddItemScreen();
}
NavigationController.PushViewController(this.addItemScreen,true);
}
}线程所执行的操作
// New Thread
[Export("ThreadLogin")]
public void ThreadLogin ()
{
using (var pool = new NSAutoreleasePool())
{
baseAlert = new UIAlertView("connecting...","",null,null,null);
baseAlert.Show();
ActView = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
ActView.Center = new PointF(baseAlert.Bounds.Size.Width/2.0f,baseAlert.Bounds.Size.Height-40.0f);
ActView.StartAnimating();
baseAlert.AddSubview(ActView);
}
}

本文介绍如何使用Monotouch在登录操作中实现同步显示警告框和进度轮,通过创建新线程并利用线程判断来确保用户体验流畅。当登录失败时能够及时给出反馈。

2496

被折叠的 条评论
为什么被折叠?



