簡體   English   中英

使用ScheduledTaskAgent更新鎖定屏幕

[英]Update lock screen using a ScheduledTaskAgent

我希望能夠使用計划任務代理更新我的鎖屏圖像。 我確實看過構建Windows Phone 8應用程序開發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次之后)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM