这是我检查用户名和密码的服务
[OperationContract]
public bool LoginCheck(string username, string password)
{
RoadTransDataContext db = new RoadTransDataContext();
var _Pass = (from d in db.users where d.username == username select d.password).SingleOrDefault();
if (_Pass == password)
{
return true;
}
else
{
return false;
}
}
这是儿童窗口
private void LoginCheckCompleted(object sender, ServiceReference.LoginCheckCompletedEventArgs e)
{
_Log = e.Result;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
ServiceReference.ServiceClient webservice = new ServiceReference.ServiceClient();
webservice.LoginCheckCompleted += new EventHandler<ServiceReference.LoginCheckCompletedEventArgs>(LoginCheckCompleted);
webservice.LoginCheckAsync(txtUserName.Text, txtPassword.Password);
if (_Log == true)
{
this.DialogResult = true;
this.Close();
}
}
问题是当LoginCheckCompleted
方法完成时OKButton_Click
方法正在调用。因此,如果它输入了正确的用户名,则传递并按下按钮,如果我再次单击窗口关闭
答案 0 :(得分:2)
Silverlight使用调用Web服务的异步模型,并且等待返回响应需要一些时间。在您的示例中,将在1-2秒后调用分配_Log = e.Result;
,而假设if (_Log == true)
将在分配之前立即调用private void LoginCheckCompleted(object sender, ServiceReference.LoginCheckCompletedEventArgs e)
{
_Log = e.Result;
if (_Log == true)
{
this.DialogResult = true;
this.Close();
}
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
ServiceReference.ServiceClient webservice = new ServiceReference.ServiceClient();
webservice.LoginCheckCompleted += new EventHandler<ServiceReference.LoginCheckCompletedEventArgs>(LoginCheckCompleted);
webservice.LoginCheckAsync(txtUserName.Text, txtPassword.Password);
}
。
这就是为什么你应该在回调中放入所有必要的代码并删除异步调用后的所有代码。我已经为你解决了这个问题:
{{1}}