【问题标题】:Command binded to the button is fired twice绑定到按钮的命令被触发两次
【发布时间】:2016-06-12 13:23:35
【问题描述】:

我的 WPF 应用程序的用户不时能够触发两次绑定到按钮的命令。

XAML 代码:

   <Button x:Name="btnAccept" 
           Style="{StaticResource FlatButtonLarge}"
           Height="42" 
           Command="{Binding Path=SubmitCmd}"
           Content="Submit" />

我还有 KeyBindings

<Window.InputBindings>
    <KeyBinding Key="F9" Command="{Binding SubmitCmd}" />
</Window.InputBindings>

我无法重现错误,但根据数据库中的更改,我断定该命令一次又一次被触发两次。它真的可行吗?我该如何防止这种现象。 SubmitCmd 将新记录添加到数据库并关闭表单。

下面是代码:

    vm.SubmitCmd = new RelayCommand(pars => DoSubmit(), pars => vm.CmdSubmitCanExecute, "Submit" );

    private void DoSubmit()
    {
        try
        {
            if (!vm.LaunchAllowed)
            {
                this.Close();
            }
            else
            {
                vm.LaunchAllowed = false;
                bool isOk = DBService.SaveToDB(vm.Dto);

                if (isOk)
                {
                    DialogResult = true;
                    this.Close();
                }
                else
                {
                    ShowError(this, result);
                    vm.LaunchAllowed = true;
                }
            }
        }
        catch (Exception ex)
        {
            ShowError(this, ex.Message);
            vm.LaunchAllowed = true;
        }
    }

和 ViewModel 代码:

    public ICommand SubmitCmd{ get; set; }

    public bool CmdSubmitCanExecute
    {
        get
        {
            return LaunchAllowed;
        }
    }

【问题讨论】:

  • 你能出示SubmitCmd的代码吗?

标签: c# wpf binding command


【解决方案1】:

我想我也看到了这个,但是 - 就像你一样 - 我无法重现它。这也是我的结论。

为了解决这个问题,我在命令运行后立即禁用了该按钮。因此,假设您使用 mvvm,请为其添加一个属性(不要忘记引发属性更改事件)并将按钮的 IsEnabled-Property 绑定到新属性

【讨论】:

  • 我已经完成了。属性 LaunchAllowed 就是这样一个属性。此属性控制是否可以触发命令。您认为这还不够吗(请参阅我的 ViewModel 代码)?
  • 好的,但是您应该在命令上触发 CanExecuteChanged-Event:
  • 您是否建议将 CommandManager.InvalidateRequerySuggested() 放在 DoSubmit() 过程的开头?
【解决方案2】:

我有同样的问题。 在我的情况下,我将一个命令绑定到两个组件中,这是错误的代码

<pv:PancakeView Grid.Row="3" CornerRadius="30" Margin="24,8,24,16" BackgroundColor="{StaticResource MainGreen}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" HasShadow="True"
                 xct:TouchEffect.PressedOpacity="0.7"
                 xct:TouchEffect.NormalOpacity="1"
                 xct:TouchEffect.AnimationEasing="{x:Static Easing.CubicInOut}"
                 xct:TouchEffect.AnimationDuration="100"
                 xct:TouchEffect.PressedScale="0.9"
                 xct:TouchEffect.Command="{Binding Checkout}">
    <Label Text="CHECKOUT" TextColor="{StaticResource MainWhite}" FontSize="18" FontFamily="{StaticResource PoppinsMedium}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
    <pv:PancakeView.Shadow>
        <pv:DropShadow Offset="0,0" Opacity="0.15" BlurRadius="25"></pv:DropShadow>
    </pv:PancakeView.Shadow>
    <pv:PancakeView.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding Checkout}"/>
    </pv:PancakeView.GestureRecognizers>
</pv:PancakeView>

在那个 xaml 代码中,我将 Checkout 命令绑定到 TapGestureRecognizerxct:TouchEffect

删除其中一个为我解决问题。

对于您的情况,我认为以下方法可行

在 KeyBindings 区域中,将绑定的命令更改为新命令,如 &lt;KeyBinding Key="F9" Command="{Binding OtherSubmitCmd}" /&gt;

public ICommand OtherSubmitCmd{ get; set; } 添加到您的 ViewModel。然后为OtherSubmitCmd分配相同的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2022-09-03
    相关资源
    最近更新 更多