C# WinRT:如何为搜索框建议创建填充一种颜色的图像文件或流
我正在编写一个windows 8.1商店应用程序,它在C# WinRT:如何为搜索框建议创建填充一种颜色的图像文件或流,c#,windows-runtime,windows-8.1,search-box,C#,Windows Runtime,Windows 8.1,Search Box,我正在编写一个windows 8.1商店应用程序,它在网格视图和顶部的搜索框中显示所有系统颜色。当我键入搜索查询时,我希望建议显示结果建议,并用一个矩形填充建议的颜色,但我无法设置DataTemplate。提供该矩形的唯一方法是将其作为irandomaccesstreamreference中的图像 那么,如何在该建议中获得一个100×100像素的矩形呢?您可以使用RandomAccessStreamReference.CreateFromStream为AppendResultsGuggestio
网格视图
和顶部的搜索框
中显示所有系统颜色。当我键入搜索查询时,我希望建议显示结果建议,并用一个矩形填充建议的颜色,但我无法设置DataTemplate。提供该矩形的唯一方法是将其作为irandomaccesstreamreference
中的图像
那么,如何在该建议中获得一个100×100像素的矩形呢?您可以使用
RandomAccessStreamReference.CreateFromStream
为AppendResultsGuggestion
创建一个IRandomAccessStreamReference,您只需要一个包含图像数据的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);