3

私はこのコードを使用しています

        Hello.Background = System.Windows.Media.Brushes.Blue;
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
        TimeSpan span = new TimeSpan(0,1,0);
        dispatcherTimer.Start(); 
        dispatcherTimer.Tick += delegate
        {

            if (dispatcherTimer.Interval > span)
            {
                Hello.Background = System.Windows.Media.Brushes.Red;
                dispatcherTimer.Stop();
            }
        };

しかし、ボタンはフェードインとフェードアウトを続けます。色を一定にしたい

C#

            private void Button_Click(object sender, RoutedEventArgs e)
    {
        Hello.Background = System.Windows.Media.Brushes.Blue;
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
        TimeSpan span = new TimeSpan(0,1,0);
        dispatcherTimer.Start(); 
        dispatcherTimer.Tick += delegate
        {

            if (dispatcherTimer.Interval > span)
            {
                Hello.Background = System.Windows.Media.Brushes.Red;
                dispatcherTimer.Stop();
            }
        };


    }

Xaml

<Button Name="Hello" Content="Hello" Background="White"  Foreground="Black " Click="Button_Click">
 </Button>
4

4 に答える 4

4

を作成し、Styleを使用してTriggerを開始するStoryboardことができますColorAnimations

例:

<Style x:Key="AnimatedButton" TargetType="Button">
    <Setter Property="Background" Value="Red" />
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetProperty="Background.Color">
                        <ColorAnimation To="Blue" Duration="0:0:4" />
                        <ColorAnimation To="Red" BeginTime="0:1:52" Duration="0:0:4" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>
于 2013-10-30T06:12:31.700 に答える
0

実際に別のアプローチを示すために... MVVMを使用している場合は、ボタンの色をViewModelのプロパティにバインドし、クリックすると2分の background/timer を実行できます。2分経ったら別の色に変わります。

xamlはあまり関係ありませんが、ここにある他のソリューションのいくつかが好きです:)

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Thread.Sleep(2000); // two second
        ButtonColorPropertyName= Colors.Red;
}

ButtonColorPropertyName( ViewModel に、または名前を付けたいものがあると仮定します)

于 2013-10-30T06:16:40.503 に答える