【发布时间】:2015-04-30 03:30:52
【问题描述】:
我有一个简单的 WCF 服务,它正在验证登录并返回一个代表用户的对象。
它正在与 Xamarin Forms PCL 项目一起运行。 当我点击登录按钮时,第一次返回的对象返回null,但第二次按下时返回用户对象。
对象正在完成方法的异步函数中创建。
我不太确定我错过了什么。
这里有一些代码可以帮助澄清。
感谢您的所有帮助!
点击按钮调用的函数...
private void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND PASSWORD RECEIVED VALIDATE AGAINST DB
//cl.ValidateUserAsync (strUserName, strPassword);
//strResult = strUserRole;
BuildClientService bcs = new BuildClientService ();
//AuthenticateUser au = new AuthenticateUser ();
TDMSClient cl = new TDMSClient ();
cl = bcs.InitializeServiceClient();
cl.ValidateUserAsync(strUserName, strPassword);
//au.AuthenticateUserLogin (strUserName, strPassword, cl);
//DisplayAlert("New User", lu.FullName, "OK");
if (lu.FullName == null || lu.FullName == "") {
DisplayAlert ("Alert", "NULL", "OK");
} else {
DisplayAlert ("Alert", lu.FullName, "OK");
}
}
构建用户对象的代码...
public class BuildClientService
{
public static readonly EndpointAddress EndPoint = new EndpointAddress("http://wcf.thompsoncs.net/TDMS.svc");
public string strUserRole = "";
BasicHttpBinding binding = CreateBasicHttp();
private static BasicHttpBinding CreateBasicHttp()
{
BasicHttpBinding binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
public TDMSClient InitializeServiceClient(){
TDMSClient cl = new TDMSClient();
cl = new TDMSClient (binding, EndPoint);
cl.ValidateUserCompleted += clOnValidateUserCompleted;
return cl;
}
private void clOnValidateUserCompleted(object sender, ValidateUserCompletedEventArgs validateUserCompletedEventArgs)
{
TDMS.Login.lu = validateUserCompletedEventArgs.Result;
}
}
谢谢!我更改了代码,现在出现 UI 线程错误... uikit 一致性错误 你正在调用一个只能从 ui 线程调用的 uikit 方法。
private async void buildLoginUser(string strUserName, string strPassword)
{
if (strUserName == null || strPassword == null) {
strResult = "Please Enter User Name and Password.";
DisplayAlert("Alert",strResult,"OK");
} else {
//IF USERNAME AND PASSWORD RECEIVED VALIDATE AGAINST DB
BuildClientService bcs = new BuildClientService ();
TDMSClient cl = new TDMSClient ();
cl = bcs.InitializeServiceClient();
var task = Task.Factory.StartNew(() => cl.ValidateUserAsync(strUserName, strPassword));
await task.ContinueWith (e => {
if (lu.FullName == null || lu.FullName == "") {
DisplayAlert ("Alert", "NULL", "OK");
} else {
DisplayAlert ("Alert", lu.FullName, "OK");
}
});
}
}
【问题讨论】:
标签: c# wcf xamarin xamarin.ios