【问题标题】:How can I stop a Device.StartTimer () when I change the page and when my app is moved to the background? and correct use of MessagingCenter当我更改页面以及将我的应用程序移至后台时,如何停止 Device.StartTimer ()?并正确使用 MessagingCenter
【发布时间】:2018-12-03 14:42:51
【问题描述】:

我正在尝试创建一个 ListView,它每 5 秒刷新一次其内容,为此我使用的是 Device.StartTimer (),我已经设法让它工作,但我注意到 Device.StartTimer () 对应用程序是全局的,即使我更改页面或更改设备上的应用程序,回调中的内容仍在运行。

我的问题是:当我更改页面和更改应用程序时,如何让计时器停止?

到目前为止,我已经取得了一些进展,即使用 OnAppearing、OnDisAppearing 和 MessagingCenter 我已经设法在计时器停止时更改页面。

有了这个实现,我担心我真的不知道我是否真的退订了消息,这是正确的地方,我也希望有人告诉我。

下面是我的 View 和我的 ViewModel 的片段:

查看:

public partial class MonitoringView : TabbedPage
{

    MonitoringViewModel context = new MonitoringViewModel();

    public MonitoringView()
    {
        InitializeComponent();
        BindingContext = context;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        MessagingCenter.Send<MonitoringView>(this, "OnAppearing");
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        MessagingCenter.Send<MonitoringView>(this, "OnDisAppearing");
    }
}

视图模型:

public class MonitoringTabsViewModel : Notificable
{
    public string IdCode { get; set; }

    public bool InPage { get; set; }

    private string description;

    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged();
        }
    }

    private ObservableCollection<PcData> sensors;

    public ObservableCollection<PcData> Sensors
    {
        get { return sensors; }
        set
        {
            sensors = value;
            OnPropertyChanged();
        }
    }


    public MonitoringTabsViewModel(string idCode, string description)
    {
        IdCode = idCode;
        Description = description;
        LoadSensors(idCode);
        MessagingCenter.Subscribe<MonitoringView>(this, "OnAppearing", (sender) =>
        {
            InPage = true;
        });
        MessagingCenter.Subscribe<MonitoringView>(this, "OnDisAppearing", (sender) =>
        {
            InPage = false;
        });
        Device.StartTimer(TimeSpan.FromSeconds(5), TimerCallBack);
    }

    private bool TimerCallBack()
    {
        if (InPage)
        {
            RefreshSensors(IdCode);
            MessagingCenter.Unsubscribe<MonitoringView>(this, "OnAppearing");
            return true;
        }
        else
        {
            MessagingCenter.Unsubscribe<MonitoringView>(this, "OnDisAppearing");
            return false;
        }
    }

    private async void LoadSensors(string idCode)
    {
        Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
    }

    private async void RefreshSensors(string idCode)
    {
        Sensors = null;
        Sensors = new ObservableCollection<PcData>(await App.WebApiManager.GetCurrentStatusDeviceAsync(idCode));
    }
}

【问题讨论】:

    标签: c# android xamarin xamarin.forms timer


    【解决方案1】:

    解决方案:

    有了这个实现,我担心我真的不知道我是否 我真的退订了这条消息

    为了测试你是否真的退订了消息,你可以在调试与VS连接的应用程序时,在RefreshSensors函数中添加breakpoint。

    然后你可以在离开MonitoringView后,在另一个页面发送与MessagingCenter.Send&lt;MonitoringView&gt;(this, "OnAppearing");相同的消息,看看breakpoint是否被触发,如果没有,则说明你已成功退订。

    在你TimerCallBack 的代码中,我认为你should not 在这里取消订阅消息。当您从另一个页面返回时,您将永远不会收到该消息,因为您已取消订阅它们。当本页为onDestory时,您可以退订消息。

     private bool TimerCallBack()
        {
            if (InPage)
            {
                RefreshSensors(IdCode);
                //MessagingCenter.Unsubscribe<MonitoringView>(this, "OnAppearing");
                return true;
            }
            else
            {
                //MessagingCenter.Unsubscribe<MonitoringView>(this, "OnDisAppearing");
                return false;
            }
        }
    

    另外,您应该将Device.StartTimer(TimeSpan.FromSeconds(5), TimerCallBack); 放入onappearing 消息中。

    public MonitoringTabsViewModel(string idCode, string description)
    {
        IdCode = idCode;
        Description = description;
        LoadSensors(idCode);
        MessagingCenter.Subscribe<MonitoringView>(this, "OnAppearing", (sender) =>
        {
            InPage = true;
            Device.StartTimer(TimeSpan.FromSeconds(5), TimerCallBack);
    
        });
        MessagingCenter.Subscribe<MonitoringView>(this, "OnDisAppearing", (sender) =>
        {
            InPage = false;
        });
    }
    

    一旦收到OnAppearing 消息,Device.StartTimer 将在页面中运行,一旦收到OnDisAppearing 消息,回调将返回 false,Device.StartTimer 将停止。

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多