【问题标题】:using background task to update live tile with webservice data使用后台任务使用 Web 服务数据更新动态磁贴
【发布时间】:2013-03-06 10:46:39
【问题描述】:

我想知道是否可以从后台任务中运行我的应用程序中的方法,而不仅仅是后台任务中的代码。我问的原因是我有一个后台任务,我想用来自网络服务器的数据更新我的动态磁贴。我将 web 服务调用放在后台任务中,如下所示:

public sealed class TileUpdater : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        ServiceReference.WebServiceSoapClient test = new ServiceReference.WebServiceSoapClient();
        var nb = await test.GetLatestDataAsync("temp");
        double temperature = nb.Value;
        string unit = nb.Unit;
        string latestTemp = " " + nb.Value + " " + nb.Unit;


        var defferal = taskInstance.GetDeferral();

        var updater = TileUpdateManager.CreateTileUpdaterForApplication();
        updater.EnableNotificationQueue(true);

        updater.Clear();

        for (int i = 1; i < 6; i++)
        {
            var tile = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText02);
            tile.GetElementsByTagName("text")[0].InnerText = "Tile " + latestTemp;
            tile.GetElementsByTagName("text")[1].InnerText = DateTime.Now.ToString("hh-mm");

            updater.Update(new TileNotification(tile));
        }

        defferal.Complete();
    }
}

但是,当我运行该应用程序时,什么都没有发生,而且我没有收到错误。似乎它在调用 Web 服务时停止。无论如何我可以让后台任务调用应用程序代码中的方法吗?

【问题讨论】:

    标签: c# .net windows-8 windows-runtime windows-store-apps


    【解决方案1】:

    这里没有太多事情要做,所以我猜测一下。我的建议 - 将 GetDeferral 线移到第一位:

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        var defferal = taskInstance.GetDeferral();
    
        ServiceReference.WebServiceSoapClient test = new ServiceReference.WebServiceSoapClient();
        var nb = await test.GetLatestDataAsync("temp");
        double temperature = nb.Value;
        string unit = nb.Unit;
        string latestTemp = " " + nb.Value + " " + nb.Unit;
    
        // move up
        // var defferal = taskInstance.GetDeferral();
        ...
    

    【讨论】:

      【解决方案2】:

      您是否尝试过在调试器中运行后台任务以确保不会发生错误? Debug Location 工具栏中有一个下拉按钮,在 Process 组合框旁边,您可以使用它。只需启动您的应用程序,然后在下拉按钮中单击您的后台任务的名称。

      它的Run 方法将立即开始执行,您将能够像调试您的应用程序代码一样对其进行调试。我想您想从任务中调用应用中现有方法的主要原因是能够正确调试它。

      您可以在this article on MSDN 中找到有关调试任务的更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多