C# 绑定到按钮的命令被激发两次
我的WPF应用程序的用户可以不时触发绑定到按钮的两次命令 XAML代码:C# 绑定到按钮的命令被激发两次,c#,wpf,binding,command,C#,Wpf,Binding,Command,我的WPF应用程序的用户可以不时触发绑定到按钮的两次命令 XAML代码: <Button x:Name="btnAccept" Style="{StaticResource FlatButtonLarge}" Height="42" Command="{Binding Path=SubmitCmd}" Content="Submit" /> 和视图模型代码: public IC
<Button x:Name="btnAccept"
Style="{StaticResource FlatButtonLarge}"
Height="42"
Command="{Binding Path=SubmitCmd}"
Content="Submit" />
和视图模型代码:
public ICommand SubmitCmd{ get; set; }
public bool CmdSubmitCanExecute
{
get
{
return LaunchAllowed;
}
}
我想我也看到了,但是——作为你——我无法复制它。这也是我的结论
为了解决这个问题,我在命令运行后立即禁用了按钮。因此,假设您使用mvvm,向它添加一个属性(不要忘记引发property changed事件),并将按钮的IsEnabled属性绑定到新属性,我想我也看到了这一点,但作为您,我无法复制它。这也是我的结论 为了解决这个问题,我在命令运行后立即禁用了按钮。因此,假设您使用mvvm,向它添加一个属性(不要忘记引发property changed事件),并将按钮的IsEnabled属性绑定到新属性,我也有同样的问题。 在我的例子中,我将一个命令绑定到两个组件中,下面是错误的代码
<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
命令绑定到TapGestureRecognizer
和xct:touchfeffect
移除其中一个,为我解决问题
对于您的情况,我认为下面的方法将起作用
在KeyBindings区域中,将绑定的命令更改为如下所示的新命令
将public ICommand和其他submitCmd{get;set;}
添加到您的ViewModel。然后为OtherSubmitCmd
分配相同的方法。我有相同的问题。
在我的例子中,我将一个命令绑定到两个组件中,下面是错误的代码
<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
命令绑定到TapGestureRecognizer
和xct:touchfeffect
移除其中一个,为我解决问题
对于您的情况,我认为下面的方法将起作用
在KeyBindings区域中,将绑定的命令更改为如下所示的新命令
将
public ICommand和其他submitCmd{get;set;}
添加到您的ViewModel。然后为其他SubmitCmd
指定相同的方法。您可以显示SubmitCmd
的代码吗?您可以显示SubmitCmd
的代码吗?我已经做过了。属性LaunchAllowed就是这样一个属性。此属性控制是否可以激发命令。您认为这还不够(请参阅我的ViewModel代码)?好的,但您应该在命令上触发CanExecuteChanged事件:您是否建议将CommandManager.InvalidateRequestSuggested()放在DoSubmit()过程的开始位置?我已经做过了。属性LaunchAllowed就是这样一个属性。此属性控制是否可以激发命令。您认为这还不够吗(请参阅我的ViewModel代码)?好的,但是您应该在命令上触发CanExecuteChanged事件:您是否建议将CommandManager.InvalidateRequestSuggested()放在DoSubmit()过程的开头?
<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>