none
用threading.timer来控制前台textblock会出现闪现 RRS feed

  • 问题

  • 我想要做这样一个效果,利用timer来实现textblock显示N秒后消失(collapse),但我在运行过程中出现闪现,请各位大大帮忙看下是什么问题?

            private void TextBlock_PointerEntered(object sender, PointerRoutedEventArgs e)
            {
                (sender as TextBlock).Foreground = new SolidColorBrush(Colors.Yellow);
                Warnning("Holding item to delete.");
            }
    
            private void Warnning(string str) {
                warning.Visibility = Visibility.Visible;
                warning.Text = str;
                timer = new Timer(new TimerCallback(async (object state) =>
                {
                    TextBlock warn = state as TextBlock;
                    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
                        () => { warn.Visibility = Visibility.Collapsed; });
                    timer.Dispose();
                }), warning, 0, 10000);
            }

    2015年9月26日 13:44

答案

  • 你好,

    >>”利用timer来实现textblock显示N秒后消失

    Winrt中,如果你需要跟UI进行交互的话,你可以使用DispatcherTimer来实现这个功能,具体信息可以参考微软官方文档:https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.dispatchertimer

    这里我做了个sample,供你参考:

    DispatcherTimer timer; int count = 0; string str = "Holding item to delete."; public MainPage() { this.InitializeComponent(); } private void TextBlock_PointerEntered(object sender, PointerRoutedEventArgs e) { (sender as TextBlock).Foreground = new SolidColorBrush(Colors.Yellow); timer = new DispatcherTimer(); timer.Tick += timer_Tick; timer.Interval = new TimeSpan(0,0,1); count = 10; timer.Start(); } void timer_Tick(object sender, object e) { if (count == 0) { timer.Stop(); warning.Visibility = Visibility.Collapsed; } else { warning.Visibility = Visibility.Visible; warning.Text = str + count; count--; } }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年9月28日 10:00
    版主