如果从主页重新初始化了当前页面,则处置Device.StartTimer。

时间:2019-07-03 17:41:00

标签: xamarin xamarin.forms

我想从App.cs页重新初始化当前页面时停止Device.StartTimer,但是每次重新初始化Device.Starttimer时都创建新实例,并且函数多次调用。

从链接和引用中,我知道直到return语句不执行,它不会停止,但是在我的情况下,我无法理解如何在方法外执行return语句。

下面是我的CS页面代码。

public partial class MediaScreen : ContentPage
    {
        public static readonly List<string> ImageExtensions = new List<string>
        { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };

        public static readonly List<string> videoExtensions = new List<string> {
            ".WAV", ".MID", ".MIDI", ".WMA", ".OGG", ".RMA", //etc
            ".AVI", ".MP4", ".DIVX", ".WMV", //etc
            };

        int i = 0;

        DisplayInfo mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
        public MediaScreen()
        {
            InitializeComponent();
            try
            {
                StartPlaying();
            }
            catch (Exception e)
            {

            }
        }

        public bool StartPlaying()
        {
             CrossMediaManager.Current.Stop();
            if (i == App.localStorage.Playlist.PlayListItems.Count)
            {
                i = 0;
                StartPlaying();
                return false;
            }

            var data = App.localStorage.Playlist.PlayListItems[i].MediaLibrary;
            string fileName = App.path + "\\" + Path.GetFileName(data.Url);

            if (ImageExtensions.Contains(Path.GetExtension(fileName).ToUpperInvariant()))
            {
                // process image
                imgPlayer.IsVisible = true;
                imgPlayer.Source = fileName;

                videoPlayer.IsVisible = false;
            }
            else
            {
                try
                {
                    CrossMediaManager.Current.Play(fileName);
                    CrossMediaManager.Current.MediaItemFinished += finishVideo;
                    videoPlayer.IsVisible = true;
                    imgPlayer.IsVisible = false;
                }
                catch (Exception e1)
                {
                }
            }
            i++;

         Device.StartTimer(TimeSpan.FromSeconds
                   (data.Duration), StartPlaying);
            return false;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以改为使用可停止计时器。它有2个目标:可以停止,可以重新安排(如果尚未开始,则可以重新安排):

计划示例:

public StoppableTimer<object> timerScheduled; //object is param type you want

public void LaunchTimer()
{
            if (timerScheduled == null)
            {
                timerScheduled = new StoppableTimer<object>(TimeSpan.FromSeconds(2), OnTimerStart);
                timerScheduled.Start(null); //whatever param
            }
            else
            {
                //relaunch
                timerScheduled.Stop();
                timerScheduled.Start(control);
            }
}

因此,当您需要取消计时器时,只需致电timerScheduled.Stop();

您的回调:

private void OnTimerStart(object p)
{
   //StartPlaying - do your stuff
}

班级:

 public class StoppableTimer<T> // T is the parameter you want to pass to timer
    {
        private readonly TimeSpan timespan;
        private readonly Action<T> callback;

        private CancellationTokenSource cancellation;

        public StoppableTimer(TimeSpan timespan, Action<T> callback)
        {
            this.timespan = timespan;
            this.callback = callback;
            this.cancellation = new CancellationTokenSource();
        }

        public void Start(T param)
        {
            CancellationTokenSource cts = this.cancellation; // safe copy
            Device.StartTimer(this.timespan,
                () => {
                    if (cts.IsCancellationRequested) return false;
                    this.callback.Invoke(param);
                    return false; // or true for periodic behavior
                });
        }

        public void Stop()
        {
            Interlocked.Exchange(ref this.cancellation, new CancellationTokenSource()).Cancel();
        }

        public void Dispose()
        {

        }
    }