我注意到了xamarin表单中的Web服务。这是我的.cs代码
static readonly EndpointAddress Endpoint = new EndpointAddress("myWebService");
IVSConnectAPIClient client;
public MainPage()
{
InitializeComponent();
BasicHttpBinding binding = CreateBasicHttpBinding();
client = new IVSConnectAPIClient(binding, Endpoint);
}
private void Button_Clicked(object sender, EventArgs e)
{
if(condition){
client.UserLoginAsync(pass parameters);
client.UserLoginCompleted += Client_UserLoginCompleted;
}
else{
DisplayAlert("Alert!", "Please enter User ID and Password to proceed.", "OK");
}
}
public void Client_UserLoginCompleted(object sender, UserLoginCompletedEventArgs e)
{
//result from web service
if(conditon){
//go to another page
}else{
DisplayAlert("Alert!", "Credential doesnt match the system", "OK");
}
所以这就是发生的事情。当我输入错误的登录ID和密码并单击按钮时,它会完美地向我显示警报(1次),但是当我点击相同的错误登录ID并传递2时,代码执行两次并显示弹出窗口2次,当我点击具有相同的不正确的登录ID并且第三次传递弹出窗口显示3次,依此类推。
有谁知道为什么会这样。
答案 0 :(得分:1)
点击每个按钮,您再次订阅UserLoginCompleted
事件。因此,每次触发事件时,都会通知每个订阅。
解决方案是只订阅一次,例如在构造函数中:
client = new IVSConnectAPIClient(binding, Endpoint);
client.UserLoginCompleted += Client_UserLoginCompleted;