我正在编写一个 Windows 8.1 商店应用程序,它在顶部显示所有系统颜色 aGridView
和 a SearchBox
。当我输入搜索查询时,我希望建议使用填充了建议颜色的矩形显示结果建议,但我无法设置 DataTemplate。提供该矩形的唯一方法是作为IRandomAccessStreamReference
.
那么,我如何在该建议中获得一个 100x100 像素的矩形?
我正在编写一个 Windows 8.1 商店应用程序,它在顶部显示所有系统颜色 aGridView
和 a SearchBox
。当我输入搜索查询时,我希望建议使用填充了建议颜色的矩形显示结果建议,但我无法设置 DataTemplate。提供该矩形的唯一方法是作为IRandomAccessStreamReference
.
那么,我如何在该建议中获得一个 100x100 像素的矩形?
您可以使用RandomAccessStreamReference.CreateFromStream
创建一个 IRandomAccessStreamReference,因为AppendResultSuggestion
您只需要一个包含图像数据的 RandomAccessStream。
为此,您可以使用以下方法:
private async Task<InMemoryRandomAccessStream> CreateInMemoryImageStream(Color fillColor, uint heightInPixel, uint widthInPixel)
{
var stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId,stream);
List<Byte> bytes = new List<byte>();
for (int x = 0; x < widthInPixel; x++)
{
for (int y = 0; y < heightInPixel; y++)
{
bytes.Add(fillColor.R);
bytes.Add(fillColor.G);
bytes.Add(fillColor.B);
bytes.Add(fillColor.A);
}
}
encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, widthInPixel, heightInPixel, 96, 96, bytes.ToArray());
await encoder.FlushAsync();
return stream;
}
之后,您可以致电:
args.Request.SearchSuggestionCollection.AppendResultSuggestion("Green", string.Empty, string.Empty, await CreateInMemoryImageStream(Colors.Green), string.Empty);
我知道它看起来很老套,但它就像一个魅力!