C# 我的按钮正在执行两次或更多次

C# 我的按钮正在执行两次或更多次,c#,wpf,mvvm,windows-phone,C#,Wpf,Mvvm,Windows Phone,我试图避免在有人双击按钮时执行异步任务两次或两次以上 当我的应用程序延迟时,我的等待导航。ShowMyButton();会去两次或更多次 private readonly ICommand MyButton; private bool? canExecuteMyButton { get; set; } public MyViewModel() { this.MyButton = new Command((nothing) =>

我试图避免在有人双击按钮时执行异步任务两次或两次以上

当我的应用程序延迟时,我的等待导航。ShowMyButton();会去两次或更多次

    private readonly ICommand MyButton;
    private bool? canExecuteMyButton { get; set; }

    public MyViewModel()
    {
        this.MyButton = new Command((nothing) =>
            {
                GoToMyButton();
            }, this.CanExecuteMyButton);
    }

    private async void GoToMyButton()
    {
        canExecuteMyButton = false;
        await _navigation.ShowMyButton();
        canExecuteMyButton = true;
    }

    private bool CanExecuteMyButton(object state)
    {
        if (canExecuteMyButton == null)
            return true;

        return (bool)canExecuteMyButton;
    }

这应该更好地发挥作用:

private async void GoToMyButton()
{
    if (canExecuteMyButton)
    {
        canExecuteMyButton = false;
        CommandManager.InvalidateRequerySuggested();
        await _navigation.ShowMyButton();
        canExecuteMyButton = true;
        CommandManager.InvalidateRequerySuggested();
    }
}

您的问题是什么?在调用
\u navigation.ShowMyButton()
之前,检查
canExecuteMyButton
是否为true如何?问题是它总是在执行。请尝试在canExecuteMyButton
设置{u canExecuteMyButton=value;MyButton.CanExecuteChanged();}