弹出窗口显示 xamarin 表单递增次数



我注意到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次, 等等。

有谁知道为什么会这样。

每次单击按钮时,您都会再次订阅UserLoginCompleted事件。因此,每次触发事件时,都会通知每个订阅。

解决方案是只订阅一次,例如在构造函数中:

client = new IVSConnectAPIClient(binding, Endpoint);
client.UserLoginCompleted += Client_UserLoginCompleted;

最新更新