2014-06-20 14 views
0

我正在将我的应用程序重新编写为适用于Windows(Phone)8.1的通用应用程序模型,并进行了优化。使用WP8.1通用应用程序在BackgroundTask(C#)中呈现自定义活动切片的最佳方式?

我创建了我的BackgroundTask(C#),它从外部源获取一些数据,最后一步是渲染可以用作tile的自定义.PNG。

以前,对于Windows Phone 7.x和8.0,我使用了名为TCD.Controls.LiveTiles.Light的第三方应用程序,它可以帮助您将XAML UserControls转换为.PNGs。这工作完美无瑕,但它似乎与通用应用程序模型有一些兼容性问题。

现在我想知道 - 在BackgroundTask中创建.PNGs的最佳方法是什么?

我读了关于XamlRenderingBackgroundTask and RenderTargetBitmap的C++实现,但由于我还没有关于C++的知识,而且我的当前任务已经在C#中,我想要使用C#。

亲切的问候, 尼尔斯Laute

+0

只需在C#中执行他们在C++示例中的操作。 :)我测试了它,它似乎工作正常。我不认为内存或CPU时间是一个问题(当我测试它时,它们不是)。 – yasen

回答

0

我有同样的问题,但发现在C#中呈现PNG的这个例子。我现在发现这个对https://social.msdn.microsoft.com/Forums/en-US/43295c90-43e8-4b08-8a25-958a1c3d0a0b/explanation-on-windowsuixamlmediaxamlrenderingbackgroundtask?forum=WindowsPhonePreviewSDK

using System; 
using System.Threading.Tasks; 
using Windows.ApplicationModel.Background; 
using Windows.Storage.Streams; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Markup; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Media.Imaging; 
using Windows.Graphics.Imaging; 
using Windows.UI.Notifications; 
using Windows.Data.Xml.Dom; 

namespace BackgroundTask 
{ 
    public sealed class AppTileUpdater : XamlRenderingBackgroundTask 
    { 
     protected override void OnRun(Windows.ApplicationModel.Background.IBackgroundTaskInstance taskInstance) 
     { 
      BackgroundTaskDeferral _deferral = taskInstance.GetDeferral(); 
      System.Diagnostics.Debug.WriteLine("OnRun called!"); 
      UpdateTileAsync(_deferral); 
     } 

     private async Task<string> ReadFile() 
     { 
      var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); 
      var file = await folder.GetFileAsync("customTile.xml"); 
      string szCustomTileXML = await Windows.Storage.FileIO.ReadTextAsync(file); 
      return szCustomTileXML; 
     } 
     private async void UpdateTileAsync(BackgroundTaskDeferral deferral) 
     { 

      var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); 
      var file = await folder.GetFileAsync("customTile.xml"); 
      string szCustomTileXML = await Windows.Storage.FileIO.ReadTextAsync(file); 

      Border tile = XamlReader.Load(szCustomTileXML) as Border; 

      if (null != tile) 
      { 
       tile.Background = new SolidColorBrush(Windows.UI.Colors.Orange); 
       Grid grid = tile.Child as Grid; 
       TextBlock text = grid.FindName("Timestamp") as TextBlock; 
       text.Text = DateTime.Now.ToString("hh:mm:ss"); 
       text = grid.FindName("TimeZone") as TextBlock; 
       text.Text = TimeZoneInfo.Local.DisplayName; 

       Image logo = grid.FindName("LogoImage") as Image; 
       var img = new BitmapImage() { CreateOptions = BitmapCreateOptions.None }; 
       img.UriSource = new Uri("ms-appx:///Assets/acorn.png"); 

       RenderTargetBitmap rtb = new RenderTargetBitmap(); 
       await rtb.RenderAsync(tile, 150, 150); 
       IBuffer pixels = await rtb.GetPixelsAsync(); 
       DataReader dReader = Windows.Storage.Streams.DataReader.FromBuffer(pixels); 
       byte[] data = new byte[pixels.Length]; 
       dReader.ReadBytes(data); 

       var outputFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFileAsync("UpdatedLiveTile.png", Windows.Storage.CreationCollisionOption.ReplaceExisting); 
       var outputStream = await outputFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); 
       BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputStream); 
       enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 96,96, data); 
       await enc.FlushAsync();     
      } 
      var TileMgr = TileUpdateManager.CreateTileUpdaterForApplication(); 
      TileMgr.Clear(); 
      var tileTemplate = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Image); 
      var tileImageAttributes = tileTemplate.GetElementsByTagName("image"); 
      XmlElement tmp = tileImageAttributes[0] as XmlElement; 
      tmp.SetAttribute("src", "UpdatedLiveTile.png"); 
      var notification = new TileNotification(tileTemplate); 
      TileMgr.Update(notification); 
      deferral.Complete(); 
     } 

     public void Run(IBackgroundTaskInstance taskInstance) 
     { 
      System.Diagnostics.Debug.WriteLine("Run called!"); 
      OnRun(taskInstance); 
     } 
    } 
} 
0

最有效的方法是使用Win2D框架和一步绘制位图的一步。这种方法将保持您的资源免费。

+0

有关如何执行此操作的任何示例? – Niels

相关问题