c# - 使用 ScheduledTaskAgent 更新锁定屏幕

标签 c# windows-phone-8

我希望能够使用计划任务代理更新我的锁屏图像。我确实看过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。

最佳答案

您没有在等待您的调用,因此 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 次之后)。

关于c# - 使用 ScheduledTaskAgent 更新锁定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549198/

相关文章:

c# - Winforms:如何在透明窗体上获取鼠标事件以进行透明控制

windows-phone-8 - Windows Phone (WNS) : Blocked channel URL 的推送消息

xaml - 如何删除 TextBlock 周围的多余空间

visual-studio - 为 Windows 8 编写的应用程序可以在 Windows Phone 8 上运行吗?

c# - 如何以编程方式提取应用名称和版本号?

c# - 使用 Linq to XML 和数组创建 XML

c# - C#.net 中的添加在某些情况下不起作用

c# - WCF 服务引用中的接口(interface)参数/返回类型

c# - 如何在 Visual Studio 2015 中使用资源文件

c# - WP8:更改BitMapImage时C#应用程序崩溃