c# - DispatcherTimer 仍然在 Stop() 之后勾选事件并设置为 null

标签 c# windows-phone-8 dispatchertimer audio-playback-agent

我正在编写一个应用程序来听音乐和看视频。在应用程序中,我有以下 2 页:1 用于播放音频播放代理 (APA) - 页面 A,1 用于播放视频(使用 MediaElement) - 页面 B。虽然 WP8 有 APA 错误(我已经在此处询问 here (我找到了一个解决方案 - 在 MediaElement 播放和关闭之前和之后两次停止 APA)。

问题是,在 A 页面,我每秒使用一个 DispatcherTimer tick 来检查 APA 实例位置,当我离开这个页面时,我有一个停止这个 DispatcherTimer 的功能。

即便如此,如果我多次导航到页面 A 和从页面 A 导航,然后导航到页面 B,1 秒后应用程序在 if (BackgroundAudioPlayer.Instance.PlayerState == .. .. 这意味着 DispatcherTimer 仍然滴答作响 ?????? 我怎样才能强制停止它 :(


我在这里发布了所有与 DispatcherTimer 相关的代码:

public DetailSongPage()
    {
        InitializeComponent();
        this.DataContext = App.Model;            
        BackgroundAudioPlayer.Instance.PlayStateChanged += APA_Instance_PlayStateChanged;                        
    }
DispatcherTimer timer = null;

private void APA_Instance_PlayStateChanged(object sender, EventArgs e)
    {
        if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
        {                
            UpdateTracking();
        }              
    }
protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);                           
        if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing || BackgroundAudioPlayer.Instance.PlayerState == PlayState.Paused)
        {   
            UpdateTracking();   
        }
    }
protected override void OnNavigatedFrom(NavigationEventArgs e)
    { 
        base.OnNavigatedFrom(e);
        timer.Stop();            
        timer.Tick -= timer_Tick;
        timer = null;            
    }
private void UpdateTracking()
    {
        sldTracking.Maximum = BackgroundAudioPlayer.Instance.Track.Duration.TotalMilliseconds;
        tbMaxTime.Text = string.Format(@"{0:mm\:ss}",BackgroundAudioPlayer.Instance.Track.Duration);
        // ^ these 2 lines update the UI for the slider and textblock 

        if (timer == null)
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += timer_Tick;
            timer.Start();
        }  
    }
    private void timer_Tick(object sender, EventArgs e)
    {
        if (this.timer != null)
        {
            try
            {                   
                if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing || BackgroundAudioPlayer.Instance.PlayerState == PlayState.Paused)
                {
                    sldTracking.Value = BackgroundAudioPlayer.Instance.Position.TotalMilliseconds;
                    tbCurrentTime.Text = string.Format(@"{0:mm\:ss}", BackgroundAudioPlayer.Instance.Position);
                }
            }
            catch
            {
                return;
            }
        }
    }

最佳答案

您可以在您的类中实现 IDispose,在此实现中,停止计时器。 然后在您离开页面时调用 Dispose。 在 Dispose 中,你应该这样写:

timer?.Stop();

关于c# - DispatcherTimer 仍然在 Stop() 之后勾选事件并设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421883/

相关文章:

c# - 如何使 WPF 中的 DispatcherTimer 事件更顺畅?

iphone - 暂停计时器上的 dispatch_source_cancel 导致 EXC_BAD_INSTRUCTION

c# - 面板 c# 中出现奇怪的白色区域

c# - 揭开神秘的自定义命令功能

windows-phone-8 - 在本地安装 Windows Phone 8 API 文档

dependency-injection - 如何使用 Autofac 在 Windows Phone 8 应用程序中注入(inject) ViewModels?

c# - 在计时器倒计时中包括小时

c# - 如何使用 Newtonsoft.Json 序列化 C# ref 字段?

c# - Windows Phone 8.1 接收短信事件

cordova - PhoneGap 应用程序在 WP8.1 上崩溃