Skip to main content
 首页 » 编程设计

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

2024年02月13日33lyhabc

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