英文:
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 await
ing 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论