在 Windows Phone 中持续更新数据的最佳方式?

发布于 2024-12-27 03:34:39 字数 324 浏览 4 评论 0原文

我正在尝试创建一个应用程序,该应用程序将从 Web API 获取数据并显示它,然后每 5 秒左右持续刷新数据,但我不知道执行此操作的最佳方法。

我的第一个想法只是一个简单的计时器,做一些类似于 这个问题,但我担心我可能会把它搞砸,并让计时器在不应该的情况下继续在后台运行(就像用户离开页面一样)。我是否担心一些实际上不会发生的事情?这是做我想做的事情的好方法,还是有更有效/安全的方法来做到这一点?

I'm trying to create an application that will fetch data from a web API and display it, then continuously refresh the data every 5 seconds or so, but I don't know the best way to go about doing this.

My first thought was just a simple timer, doing something like what was done in this question, but I'm worried that I may mess it up and have the timer continue running in the background when it shouldn't be (like if the user leaves the page). Am I worried over something that isn't going to actually happen? Is this a good way to go about doing what I'm trying to do, or is there a more efficient/safe way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2025-01-03 03:34:39

当您导航到应用程序外部时,计时器将不会继续,但是当您导航到应用程序内的另一个页面时,计时器将继续。您可以通过这种方式防止它:

    System.Windows.Threading.DispatcherTimer dt;

    public MainPage()
    {
        InitializeComponent();
        dt = new System.Windows.Threading.DispatcherTimer();
        dt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
        dt.Tick += new EventHandler(dt_Tick);
    }

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        dt.Stop();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        dt.Start();
    }

    void dt_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Add(listBox1.Items.Count + 1); // for testing
    }

    private void PageTitle_Tap(object sender, GestureEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); // for testing
    }

此外,如果您只是检查大多数时间未更改的数据,请考虑使用 推送通知

When you navigate outside app, timer will not continue, but when you navigate to another page inside app, timer will continue. You can prevent it this way:

    System.Windows.Threading.DispatcherTimer dt;

    public MainPage()
    {
        InitializeComponent();
        dt = new System.Windows.Threading.DispatcherTimer();
        dt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
        dt.Tick += new EventHandler(dt_Tick);
    }

    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        dt.Stop();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        dt.Start();
    }

    void dt_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Add(listBox1.Items.Count + 1); // for testing
    }

    private void PageTitle_Tap(object sender, GestureEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); // for testing
    }

Moreover, if you are just checking for data which most time is not changed, consider using push notifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文