【问题标题】:Simple Blinking Text简单的闪烁文本
【发布时间】:2019-06-19 03:37:50
【问题描述】:

我正在尝试用 C#/WPF 中尽可能少的代码组合一个闪烁“Hello World”文本的简单演示项目。我所写的内容编译并运行,但实际上并没有使文本闪烁(它基于每 2 秒触发一次并更改标签可见性的计时器。关于为什么文本不闪烁或更有效的任何想法方法会是什么?代码:

using System;
using System.Timers;
using System.Windows;

namespace BlinkingText
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        static Timer _timer;
        static MainWindow window = new MainWindow();

    public MainWindow()
    {
        InitializeComponent();

        Start();
    }

    public static void Start()
    {
        var timer = new Timer(2000);
        timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        timer.Enabled = true;
        _timer = timer;
    }

    public static void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {

        if (window.HelloWorldLabel.Visibility == Visibility.Hidden)
        {
            Application.Current.Dispatcher.Invoke((Action)delegate
            {
                window.HelloWorldLabel.Visibility = Visibility.Visible;
            });
        }
        else
        {
            Application.Current.Dispatcher.Invoke((Action)delegate
            {
                window.HelloWorldLabel.Visibility = Visibility.Hidden;
            });
      }
    }
  }
}

【问题讨论】:

  • 您应该能够完全在 XAML 中完成此操作,而无需 C# 代码;创建一个AnimationTimeline 并将文本不透明度从 0% 更改为 100%,然后延迟返回 0%。
  • 此外,在 WPF 应用程序中,您将使用 DispatcherTimer 来避免调用 Dispatcher.Invoke。 Start 方法和 Timer 和 Window 字段都不应该是静态的。

标签: c# wpf xaml


【解决方案1】:

应用程序中的文本不会闪烁,因为您在隐藏的 MainWindow 实例中更改了 Label 的 Visibility,这与您在屏幕上看到的不同。

它是由

创建的
static MainWindow window = new MainWindow();

但从未显示。

除此之外,您应该像这样使用 DispatcherTimer:

public partial class MainWindow : Window
{
    private readonly DispatcherTimer timer =
        new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };

    public MainWindow()
    {
        InitializeComponent();

        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        HelloWorldLabel.Visibility = HelloWorldLabel.Visibility == Visibility.Visible
            ? Visibility.Hidden : Visibility.Visible;
    }
}

您也可以完全在 XAML 中执行类似的操作,例如像这样:

<TextBlock Text="Hello, World.">
    <TextBlock.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames
                        Storyboard.TargetProperty="Visibility"
                        Duration="0:0:4" RepeatBehavior="Forever">

                        <DiscreteObjectKeyFrame
                            KeyTime="0:0:0"
                            Value="{x:Static Visibility.Visible}"/>
                        <DiscreteObjectKeyFrame
                            KeyTime="0:0:2"
                            Value="{x:Static Visibility.Hidden}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </TextBlock.Triggers>
</TextBlock>

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多