首页 > 解决方案 > 显示 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 次,依此类推。

有谁知道为什么会这样。

标签: c#xamarinxamarin.forms

解决方案


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

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

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

推荐阅读