后台服务以Xamarin形式连续运行

时间:2018-12-07 07:33:28

标签: c# xamarin.forms background-service

我是xamarin表格的新手。我正在编写一个应用程序,我需要创建一个允许连续调用api来检查数据更改的函数,如果有任何更改,我会处理。 我正在寻找解决方案,但是什么也没有,请帮助我:( 线程是个主意吗?

2 个答案:

答案 0 :(得分:1)

好的,首先您需要轮询API以便接收需要检查的数据。为此,您可以实现我的PollingTimer.cs类:

using System;
using System.Threading;
using Xamarin.Forms;

namespace CryptoTracker.Helpers
{
    /// <summary>
    /// This timer is used to poll the middleware for new information.
    /// </summary>
    public class PollingTimer
    {
        private readonly TimeSpan timespan;
        private readonly Action callback;

        private CancellationTokenSource cancellation;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:CryptoTracker.Helpers.PollingTimer"/> class.
        /// </summary>
        /// <param name="timespan">The amount of time between each call</param>
        /// <param name="callback">The callback procedure.</param>
        public PollingTimer(TimeSpan timespan, Action callback)
        {
            this.timespan = timespan;
            this.callback = callback;
            this.cancellation = new CancellationTokenSource();
        }

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

        /// <summary>
        /// Stops the timer.
        /// </summary>
        public void Stop()
        {
            Interlocked.Exchange(ref this.cancellation, new CancellationTokenSource()).Cancel();
        }
    }
}

现在您已经向项目添加了轮询计时器,现在必须进入要从中进行轮询的内容页面。这是您的内容页面应为什么样的伪代码:

namespace YourApp.Views
{
    public class MainPage : ContentPage
    {

        PollingTimer timer;
        public MainPage ()
        {
            //PUT UI CODE HERE

            Content = layout;
            //Instantiate Polling timer to call handleaction every 5 seconds
            timer = new PollingTimer(TimeSpan.FromSeconds(5), HandleAction);

        }


        /// <summary>
        /// When the page enters the users view, this procedure is called.
        /// </summary>
        protected override void OnAppearing()
        {
            base.OnAppearing();
            //Handle action and start your timer
            HandleAction();
            timer.Start();
        }

        /// <summary>
        /// When the page disappears from the users view this procedure is called.
        /// </summary>
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            //Stop your timer
            timer.Stop(); //Stop the timer
        }


        /// <summary>
        /// Callback for the timer.
        /// </summary>
        void HandleAction()
        {
            //Make call to your api to get data
            //Compare data with data you currently have
            // Do whatever you want.
        }

希望这对您有所帮助。让我知道您是否需要更多帮助:)

答案 1 :(得分:0)

您可以使用Timer Class来发行。