为什么在 .Net MAUI 中快速点击按钮会多次触发点击事件?

huangapple go评论65阅读模式
英文:

Why do rapid clicks on button calls click event multiple times in .Net MAUI?

问题

我正在使用以下代码行导航到应用程序中的新页面:

await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));

如果用户快速多次按下按钮,导航会导致多个页面同时打开,从而导致用户体验不佳。

我们可以通过在导航到特定页面之前对导航堆栈应用以下检查来避免这种情况:

if (Shell.Current.Navigation.NavigationStack.Count == 0 || Shell.Current.Navigation.NavigationStack.Last().GetType() != typeof(MyAccountsTabbedPage))
    await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));

或者,可以在按钮上使用 isEnabled 属性的逻辑(这不太好)。

在按钮点击时,请参考以下代码:

private async void ViewStatements_Clicked(object sender, EventArgs e) {
  var button = (Button) sender;
  _accountsViewModel.SelectedLease = (LeaseInfo) button.CommandParameter;
  string tcode = _accountsViewModel.SelectedLease.TenantCode;
  await _accountsViewModel.IsServiceChargeVisible(tcode);
  if (Shell.Current.Navigation.NavigationStack.Count == 0 || Shell.Current.Navigation.NavigationStack.Last().GetType() != typeof (MyAccountsTabbedPage))
    await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));
}

这似乎不是一个好的方法,是否有更好的方法或更好的导航方法可以避免这种情况?

如果用户多次按下按钮以导航到某个屏幕,行为应与单击相同。

英文:

I am Navigating to a new page in the application using the following line of code:

  await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));

Navigation on button clicks causes multiple pages to open all at once if the button is pressed rapidly multiple times, resulting in poor user experience.

We can avoid it by applying the following check on Navigation stack before navigating to the particular page:

if (Shell.Current.Navigation.NavigationStack.Count == 0 || Shell.Current.Navigation.NavigationStack.Last().GetType() != typeof(MyAccountsTabbedPage))
            await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));

OR, by using logic on isEnabled in button (doesn't sound to be a good approach).

Refer to the following code on button click:

private async void ViewStatements_Clicked(object sender, EventArgs e) {
  var button = (Button) sender;
  _accountsViewModel.SelectedLease = (LeaseInfo) button.CommandParameter;
  string tcode = _accountsViewModel.SelectedLease.TenantCode;
  await _accountsViewModel.IsServiceChargeVisible(tcode);
  if (Shell.Current.Navigation.NavigationStack.Count == 0 || Shell.Current.Navigation.NavigationStack.Last().GetType() != typeof (MyAccountsTabbedPage))
    await Shell.Current.Navigation.PushAsync(new MyAccountsTabbedPage(_accountsViewModel, tcode, _supportPageViewModel, _contactusPageViewModel, _faqCategoryPageViewModel));

}

This doesn't seem to be a good approach, is there a better way of doing this or a better approach to do navigation to avoid it?

In case, the user presses the button multiple times to navigate to a certain screen, the behavior should be same as single click.

答案1

得分: 3

你可能遇到这个问题,因为在按钮点击引发的事件和PushAsync指令之间,您正在使用await等待代码。这会导致延迟导航到新页面。

您可以在其他地方调用这一行

await _accountsViewModel.IsServiceChargeVisible(tcode);

也许在MyAccountsTabbedPage的初始化中?

否则,如果您需要在按下按钮时立即调用它,您可以像您建议的那样使用isEnabled(这不一定是一种不好的做法),并显示一个ActivityIndicator

英文:

You're probably experiencing this issue because you're awaiting code between the event raised by the button click and the PushAsync instruction. This results in a delay navigating to the new page.

You could call this line

  await _accountsViewModel.IsServiceChargeVisible(tcode);

somewhere else (maybe in the initialization of MyAccountsTabbedPage?).

Otherwise, if you need to call it exactly when you press the button, you could use isEnabled as you suggested (it's not necessarily a bad practice) and show an ActivityIndicator.

huangapple
  • 本文由 发表于 2023年6月15日 19:48:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定