Prevent double-click from double firing a command

前端 未结 14 1819
情书的邮戳
情书的邮戳 2020-12-15 08:34

Given that you have a control that fires a command:

Is there a way to prevent the command from being fired

14条回答
  •  悲&欢浪女
    2020-12-15 09:24

    We solved it like this... with async we couldn't find any other way to effectively block extra clicks on the button which invokes this Click:

    private SemaphoreSlim _lockMoveButton = new SemaphoreSlim(1);
    private async void btnMove_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        if (_lockMoveButton.Wait(0) && button != null)
        {
            try
            {                    
                button.IsEnabled = false;
            }
            finally
            {
                _lockMoveButton.Release();
                button.IsEnabled = true;
            }
        }
    }
    

提交回复
热议问题