c# - 显示 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;
推荐阅读
- php - 自己调用函数时出错,VCS say="未定义函数"
- spring-boot - Springboot 微服务并发调用过滤器
- html - 如何仅使用 CSS 设置悬停动画?
- github - 为什么 Github Actions 对在我的 javascript 中使用 process.env 访问的任何 .env 值返回 null?
- html - 如何根据父 div 是否为 display:none 创建 CSS 规则
- c++ - 在命名空间列表中调用同名函数
- json - 如何解决 simplejson.errors.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)?
- flutter - 如果行数在限制范围内,如何禁用扩展图标?扑
- amazon-web-services - 将 jar 文件传输到远程 EC2 服务器
- amazon-web-services - AWS Quicksight - 来自多个数据库的 MySQL UNION