2012-03-21 70 views
2

我製作了一個使用實時拼貼的應用程序。我得到它與backgroundtask工作,它顯示了正確的消息,但圖像是全黑的...我的瓷磚創建和加載有什麼問題?

這裏是我的代碼,我將圖像保存到/Shared/ShellContent/

 public MainPage() 
     { 
      InitializeComponent(); 

      CurrentPlaceList = new ObservableCollection<CurrentPlaceListItemModel>(); 

      using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
        var bmp = new WriteableBitmap(173, 173); 

        var weatherImage = new BitmapImage(new Uri("/Images/WeatherIcons/01d.png", UriKind.Relative)); 

        var img = new Image { Source = weatherImage }; 

        weatherImage.CreateOptions = BitmapCreateOptions.None; 

        var tt = new TranslateTransform(); 
        tt.X = 0; 
        tt.Y = 0; 

        bmp.Render(img, tt); 

        bmp.Invalidate(); 

        var filename = "/Shared/ShellContent/01d.jpg"; 
        using (var st = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, store)) 
        { 
         bmp.SaveJpeg(st, 173, 173, 0, 100); 
        } 
      } 

      StartAgent(); 
     } 

那裏是我的代碼在我的ScheduledTask應該更新的文字和圖像,這些圖像是全黑:(

 protected override void OnInvoke(ScheduledTask task) 
     { 
      //TODO: Add code to perform your task in background 
      UpdateAppTile("-99"); 

      NotifyComplete(); 
     } 

     private void UpdateAppTile(string message) 
     { 
      ShellTile appTile = ShellTile.ActiveTiles.FirstOrDefault(); 

      if (appTile != null) 
      { 
       StandardTileData tileData = new StandardTileData 
       { 
        Title = message, 
        BackgroundImage = new System.Uri("isostore:/Shared/ShellContent/01d.jpg", System.UriKind.Absolute) 
       }; 

       appTile.Update(tileData); 
      } 
     } 

這是從幾個教程採取,任何人都可以把手指上有什麼不好?

+0

也許你需要等待'ImageLoaded'事件再渲染到'bmp'或者只是將圖像複製到'/ Shared/ShellContent /'目錄而不創建額外的'Image'對象。另外,您可以查看「獨立存儲」以查看此處保存的內容 – Ku6opr 2012-03-21 09:32:26

回答

1

對我來說,它看起來像圖像沒有足夠的時間加載。我覺得這裏的更好的辦法是做這樣的事情:

 StreamResourceInfo streamImage = Application.GetResourceStream(uri.Uri); 
     BitmapImage bitmapImage = new BitmapImage(); 
     bitmapImage.SetSource(streamImage.Stream); 
     Image image = new Image() { Width = uri.Width, Height = uri.Height, Source = bitmapImage }; 

此外,您還可以檢查出MSP工具包,可以爲您生成磚(msptoolkit上的NuGet)。