【问题标题】:Background Service run continuously in Xamarin Forms后台服务在 Xamarin Forms 中连续运行
【发布时间】:2019-05-08 23:02:36
【问题描述】:

我是 xamarin 表单的新手。我正在编写一个应用程序,我需要创建一个函数,允许连续调用 api 来检查数据的变化,如果有任何变化,我会处理一些事情。 我正在寻找解决方案,但没有,请帮助我:( 线程是一个想法?

【问题讨论】:

    标签: c# xamarin.forms background-service


    【解决方案1】:

    您可以使用Timer Class 处理您的问题。

    【讨论】:

      【解决方案2】:

      好的,首先您需要轮询 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.
              }
      

      我希望这对你有帮助。如果您需要更多帮助,请告诉我:)

      【讨论】:

      • 感谢您的回复 :D,我只是度过了我的周末,我明天会检查它,非常感谢您 :D
      • 谢谢你,它对我帮助很大,但我对更新 listview ItemSource 的方式感到困惑。当列表没有记录时,我在我的服务器上创建一个新记录,它可以更新但我继续创建另一个记录,它不能。
      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 2018-10-18
      • 2019-10-22
      • 1970-01-01
      • 2021-01-01
      • 2016-08-14
      • 1970-01-01
      • 2020-10-27
      相关资源
      最近更新 更多