【问题标题】:Update lock screen using a ScheduledTaskAgent使用 ScheduledTaskAgent 更新锁定屏幕
【发布时间】:2013-03-21 23:32:05
【问题描述】:

我希望能够使用计划任务代理更新我的锁屏图像。我确实看过Building Windows Phone 8 Apps Development Jump Start,这是一篇不错的文章。 我的问题是在这个视频中,它展示了如何使用隔离存储中的图片更改背景。 使用:

  Uri imageUri = new Uri("ms-appdata:///local/shared/shellcontent/background2.png",                           UriKind.RelativeOrAbsolute);

这不是我的情况(我需要从网络服务下载它)。 我用一段代码构建了一个小项目,它应该下载一个图像,将其存储到我的隔离存储中,然后用它来上传我的锁屏(我想这是做我想做的最好的方式。)。

为此,我使用了:

protected override void OnInvoke(ScheduledTask task)
{
    Deployment.Current.Dispatcher.BeginInvoke(() =>
      {
          SavePictureInIsolatedStorage(
              new Uri(
                  "http://www.petfinder.com/wp-content/uploads/2012/11/101418789-cat-panleukopenia-fact-sheet-632x475.jpg"));
        
         // LockHelper();
          NotifyComplete();
      });

}

还有:

private async void SavePictureInIsolatedStorage(Uri backgroundImageUri)
{

    BitmapImage bmp = new BitmapImage();
    await Task.Run(() =>
                       {
                        
                           var semaphore = new ManualResetEvent(false);
                           Deployment.Current.Dispatcher.BeginInvoke(()=>  
                                   {
                                       bmp = new BitmapImage(backgroundImageUri);
                                       semaphore.Set();
                                   });
                           semaphore.WaitOne();
                       });
    bmp.CreateOptions = BitmapCreateOptions.None;
    WriteableBitmap wbmp = new WriteableBitmap(bmp);

    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        var file = "shared/shellcontent/lockscreen.png";
        // when file exists, delete it
        if (myIsolatedStorage.FileExists(file))
        {
            myIsolatedStorage.DeleteFile(file);
        }

        using (var isoFileStream = new IsolatedStorageFileStream(file, FileMode.Create, myIsolatedStorage))
        {
            // use ToolStackPNGWriterExtensions
            ToolStackPNGWriterLib.PNGWriter.WritePNG(wbmp, isoFileStream);

        }

    }

}

我的问题是我的位图图像似乎没有被下载。 我也尝试使用 WebClient,但结果相同。

【问题讨论】:

    标签: c# windows-phone-8


    【解决方案1】:

    您没有在等待您的来电,因此NotifyComplete() 将在任何有机会运行之前被调用。您可以通过将 lambda 函数声明为 async 来解决此问题。

    protected override void OnInvoke(ScheduledTask task)
    {
        Deployment.Current.Dispatcher.BeginInvoke(async () =>
        {
          await SavePictureInIsolatedStorage(
              new Uri(
                  "http://www.petfinder.com/wp-content/uploads/2012/11/101418789-cat-panleukopenia-fact-sheet-632x475.jpg"));
    
          NotifyComplete();
         });
    }
    

    但是请注意,您的方法运行时间不会太长,否则您的计划任务将不会再次被计划(在 2 次此类失败之后)。

    【讨论】:

    • 另外,一般来说,避免async void;除非您正在编写事件处理程序,否则请使用 async Task
    • 更具体地说,您在 BeginInvoke 中的 lambda 是异步无效的。阅读 PFX 团队博客blogs.msdn.com/b/pfxteam/archive/2012/02/08/10265476.aspx 中的详细信息
    • @AlexSorokoletov - 当然 lambda 是异步无效的,因为我无法修改 BeginInvoke API。真正的问题是 OP 代码中的SavePictureInIsolatedStorage 方法是async void,而它应该是async Task。见msdn.microsoft.com/en-us/magazine/jj991977.aspx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多