我正在将我的应用程序重新编写为Universal App模型,并针对Windows(手机)8.1进行了优化。
我创建了BackgroundTask(C#),它从外部源获取一些数据,最后一步是渲染一个可以用作tile的自定义.PNG。
以前,对于Windows Phone 7.x和8.0,我使用了名为TCD.Controls.LiveTiles.Light的第三方应用,可以帮助您将XAML UserControls转换为.PNG。这完美无缺,但它似乎与Universal App模型存在一些兼容性问题。
现在我想知道 - 在BackgroundTask中创建.PNG的最佳方法是什么?
我读到了XamlRenderingBackgroundTask and RenderTargetBitmap 的C ++实现,但由于我不了解C ++,而且我当前的任务已经在C#中,我想使用C#。
亲切的问候, 尼尔劳特
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
现在最有效的方法是使用Win2D Framework并逐步绘制位图。这种方法可以保证您的资源免费。