c# – 如何在页面中创建Microsoft Band自定义图标 – Windows UWP

我一直在尝试将自定义图标添加到填充面板中.

documentation中,第8.5节中有一些信息,SDK中的示例表明开发人员可以自定义磁贴的大图标和小图标.但是,没有明确的示例说明如何在页面上的图块中为自定义图标执行此操作.

进一步搜索,我发现Visual Studio有一个新的Microsoft Band Tile Design Plugin.这似乎说明了我想要做的事情但是当我尝试使用代码生成部分(页面中间的一半)中指定的代码时,我无法加载我使用设计器创建的简单自定义布局:

《c# – 如何在页面中创建Microsoft Band自定义图标 – Windows UWP》

以下是链接自定义布局的代码,如Tile Design Plugin页面所述:

try
            {
                // create a new Guid for the tile
                tileGuid = Guid.NewGuid();
                // create a new tile with a new Guid
                WriteableBitmap smallIconBitmap = new WriteableBitmap(24, 24);
                BandIcon smallIcon = smallIconBitmap.ToBandIcon();
                WriteableBitmap tileIconBitmap = new WriteableBitmap(48, 48);
                BandIcon tileIcon = tileIconBitmap.ToBandIcon();
                BandTile tile = new BandTile(tileGuid)
                {
                    // Name of the Tile
                    Name = "MyTile",
                    // Create the small and tile icons from writable bitmaps.
                    // Small icons are 24x24 pixels.
                    SmallIcon = smallIcon,
                    // Tile icons are 46x46 pixels for Microsoft Band 1, and 48x48 pixels
                    // for Microsoft Band 2.
                    TileIcon = tileIcon
                };
                var customtiledesign = new SentimentFeedbackLayout();
                tile.PageLayouts.Add(customtiledesign.Layout);
                await customtiledesign.LoadIconsAsync(tile);

                if (await bandClient.TileManager.AddTileAsync(tile))
                {
                    Debug.WriteLine("New tile added | GUID: " + tileGuid);
                }

                PageData pd = new PageData(tileGuid, 1, customtiledesign.Data.All);

                if (await bandClient.TileManager.SetPagesAsync(tileGuid, pd))
                {
                    Debug.WriteLine("Added pages");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

错误日志如下:

Band Connected : MSFT Band 2 bb:bc
Version: 2.0.4215.0, Hardware: 26
Band - Removing all Tiles
Removed tile: MyTile
New tile added | GUID: 4803e0fe-2da2-4efb-9389-bde3a9289d30
Exception thrown: 'Microsoft.Band.BandOperationException' in mscorlib.ni.dll
Error Device status code: 0xA0CC006A received.

我在网上找不到任何错误的细节,但我认为它是因为我使用过:

 PageData pd = new PageData(tileGuid, 1, customtiledesign.Data.All);

而不是将customtiledesign.Data.All传递给… SetPagesAsync()方法.

这是因为没有重载形式的SetPageAsync将PageElementData []作为参数.

最佳答案 图标在图块布局上不可见的原因是因为索引1传递给构造函数:PageData pd = new PageData(tileGuid,1,customtiledesign.Data.All);.它应该是0.

即您的磁贴可能有多个布局,您的调用是为第二个不存在的布局设置数据.对于您添加的唯一布局,索引为0.

当你成功的时候,你可能会发现你的瓷砖没有好的图标.这是因为在调用ToBandIcon之前,smallIconBitmap和tileIconBitmap需要一些好的源代码.示例提供以下用于加载图标的代码.

    private async Task<BandIcon> LoadIcon(string uri)
    {
        StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));

        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
        {
            WriteableBitmap bitmap = new WriteableBitmap(1, 1);
            await bitmap.SetSourceAsync(fileStream);
            return bitmap.ToBandIcon();
        }
    }
点赞