【问题标题】:Click button twice to confirm action单击按钮两次以确认操作
【发布时间】:2015-07-28 20:30:43
【问题描述】:

所以我想找到一种方法,让用户通过单击两次按钮来确认他们的操作。 第一次它会通过隐藏标签显示警告并启动一个计时器,该计时器将计数几秒钟。然后,如果用户再次单击按钮(在超时之前),它将执行代码。 如果离开,标签将隐藏,用户必须再次单击 2 次才能运行代码。 最好的方法是否涉及 if 参数和全局变量。当我在想这样的事情时 (我知道它不在 c# 中,但它是我正在谈论的内容的一个很好的视觉表示):

Var buttonclick = 0
if buttonclick = 0
{Show warning label
Start timer 
Set Var buttonclick = 1}
If buttonclick = 1
{Do action}

或者还有其他更好的方法吗?

【问题讨论】:

  • 在计算机中,这称为“双击”。
  • 听起来可用性很差 - 使用标准,并显示一个是-否消息框,从而使用户确认他的操作。
  • 更好的方法:在第一次点击时执行该操作,但提供一种撤消该操作的方法,以防它不是用户想要的。
  • 不幸的是,无法撤消它,因为它会闪烁并且 android 设备返回到库存操作系统。我想避免是/否消息框,因为它会破坏应用程序的流程,我希望它尽可能流畅。而且我知道它被称为双击,但我发誓我已经在某个地方看到过它在我不记得在哪里之前使用过......
  • 我建议不要使用 global 变量,而是编写自己的自定义控件来在内部处理计时器逻辑并在第二次单击时调用Click 事件。这样,自定义点击逻辑对于自定义按钮控件之外的代码将是透明的。

标签: c# wpf confirmation


【解决方案1】:

我不确定使用计时器是否是个好主意,但我使用三态 ToggleButton 来实现类似的行为。

在我的解决方案中,当用户单击我的按钮(我称之为ConfirmCancelButton)时,它会变为红色并等待第二次单击,如果发生第二次单击,则我的“取消命令”将执行,但如果按钮丢失它是焦点(例如用户点击其他地方)它将回到原来的状态。

这是一个简单的例子:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<Window.Resources>
    <Style x:Key="ConfirmCancelButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="IsThreeState" Value="True"/>
        <Style.Triggers>
            <!-- Button is not clicked (original state) -->
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Foreground" Value="{DynamicResource TextBrush}"/>
            </Trigger>
            <!-- Button clicked once (waiting for 2nd click)-->
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Foreground" Value="Orange"/>
            </Trigger>
            <!-- Button clicked twice (executing cancel command)-->
            <Trigger Property="IsChecked" Value="{x:Null}">
                <Setter Property="Foreground" Value="Red"/>
            </Trigger>
            <!-- Button lost it's focus (back to the original state)-->
            <Trigger Property="IsFocused" Value="False">
                <Setter Property="IsChecked" Value="False"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>


<Window>
    <Grid>
        <ToggleButton Grid.Row="1" Style="{StaticResource ConfirmCancelButton}" Content="Cancel" >
            <i:Interaction.Triggers>
                <!-- if IsChecked=="{x:Null}" -->
                <i:EventTrigger EventName="Indeterminate">
                    <i:InvokeCommandAction Command="{Binding CancelCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ToggleButton>
    </Grid>
</Window>

【讨论】:

  • 这正是我想要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多