簡體   English   中英

x秒后釋放按鈕

[英]Button release after x seconds

在我的應用程序中,我需要一個可以按下一次的按鈕,它將其狀態和外觀更改為按下狀態。 1秒鍾后,應釋放按下的按鈕。

我嘗試使用ToggleButton和CheckBox進行此操作。 但這不是我所需要的。

示例:我按下按鈕。 矩形的顏色已更改。 1秒鍾后,釋放按鈕。

有什么建議么?

更新:該按鈕應該播放聲音。 如果我使用ToggleButton或CheckBox,則該聲音會播放兩次,因為該按鈕被選中了兩次。

我想補充一點:考慮使用Task而不是Timer。 好的一面,這意味着您無需在DispatchTimer之后清理並停止它; 不利的一面,這意味着您需要在GUI對象上使用Invoke()而不是直接更改它(因為它將從非GUI線程運行)。

這是一些簡單的代碼,只是禁用/啟用瞬時按鈕來說明:

private void btnTest_Click(object sender, EventArgs e)
{
    btnTest.Enabled = false;
    Task t = new Task(async () =>  { await Task.Delay(2000); EnableButton(); });
    t.Start();
}
private void EnableButton()
{
    btnTest.Invoke((MethodInvoker)delegate { btnTest.Enabled = true; });
}

編輯:更改為使用Task.Delay而不是Thread.Sleep()。

也許您可以使用間隔為1000(1秒)的計時器事件? 就像您按下按鈕一樣,觸發計時器事件,並在1秒過后松開按鈕。

最好的選擇是返回並使用ToggleButton,但在1秒鍾后將其重置。 那就是它的目的。 為您的ToggleButton創建一個click事件,並創建一個DispatcherTimer來處理延遲的切換。

    //Reference
    using System.Windows.Timers;

    //Toggle Button Click Event
    private void button_Click(object sender, RoutedEventArgs e)
    {
        button.IsChecked = true;

        //Create New DispatcherTimer
        DispatcherTimer dst = new DispatcherTimer();
        dst.Interval = new TimeSpan(0, 0, 1);  //Set Interval to 1 second.       
        dst.Tick += Dst_Tick;                   
        dst.Start(); //Start Timer  
    }

    //Timer Elapsed Event
    private void Dst_Tick(object sender, EventArgs e)
    {
        DispatcherTimer t = (DispatcherTimer)sender; //Grab the DispatcherTimer
        t.Stop(); //Stop the timer or it will keep looping
        button.IsChecked = false; //Reset the ToggleButton          
    }

我根據您的要求編寫了一個測試應用程序。 我的Xaml文件如下所示

<Window x:Class="toggleButtonTimer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:toggleButtonTimer"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <TextBlock x:Name="_value"></TextBlock>
        <ToggleButton Width="100" Height="100" x:Name="_toggle" Click="_toggle_Click" IsChecked="False"></ToggleButton>
    </StackPanel>
</Grid>

后面的代碼如下(從Tronald ..修改了代碼。)

    using System;
    using System.Windows;
    using System.Windows.Threading;
 ....


    private int value = 0;
    private void _toggle_Click(object sender, RoutedEventArgs e)
    {
        _toggle.IsChecked = true;

        //Create New DispatcherTimer
        DispatcherTimer dst = new DispatcherTimer();
        dst.Interval = new TimeSpan(0, 0, 1);  //Set Interval to 1 second.       
        dst.Tick += Dst_Tick;
        dst.Start(); //Start Timer  

        value++;
        _value.Text = value.ToString();
    }

    //Timer Elapsed Event
    private void Dst_Tick(object sender, EventArgs e)
    {
        DispatcherTimer t = (DispatcherTimer)sender; //Grab the DispatcherTimer
        t.Stop(); //Stop the timer or it will keep looping
        _toggle.IsChecked = false; //Reset the ToggleButton    

        value--;
        _value.Text = value.ToString();
    }

每次觸發單擊時,TextBlock會將變量計數+1,並且當計時器過去時,其計數為-1。 因此,textBlock在一個循環后保持為0。 這對我來說很好。 因為最后的結果為0。所以該事件僅處理一次。

它適用於Click事件和Checked事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM