尝试从UWP应用程序向Microsoft Band添加自定义磁贴

本文关键字:添加 Band 自定义 Microsoft UWP 应用程序 | 更新日期: 2023-09-27 18:25:54

我想通过适用于Windows Phone的UWP应用程序中的Microsoft Band SDK向Microsoft Band添加自定义磁贴。这是我的示例代码。

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        try
        {
            // Get the list of Microsoft Bands paired to the phone.
            var pairedBands = await BandClientManager.Instance.GetBandsAsync();
            if (pairedBands.Length < 1)
            {
                Debug.WriteLine("This sample app requires a Microsoft Band paired to your device.Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.");
                return;
            }
            // Connect to Microsoft Band.
            using (var bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
            {
                // Create a Tile with a TextButton on it.
                var myTileId = new Guid("12408A60-13EB-46C2-9D24-F14BF6A033C6");
                var myTile = new BandTile(myTileId)
                {
                    Name = "My Tile",
                    TileIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconLarge.png"),
                    SmallIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconSmall.png")
                };
                // Remove the Tile from the Band, if present. An application won't need to do this everytime it runs. 
                // But in case you modify this sample code and run it again, let's make sure to start fresh.
                await bandClient.TileManager.RemoveTileAsync(myTileId);
                // Create the Tile on the Band.
                await bandClient.TileManager.AddTileAsync(myTile);
                // Subscribe to Tile events.
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
    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();
        }
    }

如果我运行这个代码,什么也没发生。该应用程序已连接到Microsoft Band,但无法添加互动程序。方法AddTileAsync(myTile);返回false,并且不向Microsoft Band添加磁贴。

如果我在Windows Phone 8.1应用程序中尝试此代码,它会起作用,但在UWP应用程序中不起作用。

有什么想法吗?

更新以下是下载的示例应用程序。也许这会有所帮助。

尝试从UWP应用程序向Microsoft Band添加自定义磁贴

也许这会有所帮助,来自MS Band 的文档

using Microsoft.Band.Tiles;
...
try
{
    IEnumerable<BandTile> tiles = await bandClient.TileManager.GetTilesAsync();
}
catch (BandException ex)
{
    //handle exception 
}
//determine if there is space for tile
try
{
    int tileCapacity = await bandClient.TileManager.GetRemainingTileCapacityAsync();
}
catch (BandException ex)
{
    //handle ex
}
//create tile
WriteAbleBitmap smallIconBit = new WriteAbleBitmap(24, 24);
BandIcon smallIcon = smallIconBit.ToBandIcon();
WriteAbleBitmap largeIconBit = new WriteAbleBitmap(48, 48);//46, 46 for MS band 1
BandIcon largeIcon = largeIconBit.ToBandIcon();
Guid guid = Guid.NewGuid();
BandTile tile = new BandTile(guid)
{
    //enable Badging
    IsBadgingEnabled = true,
    Name = "MYNAME"
    SmallIcon = smallIcon;
    TileIcon = largeIcon;
};
try
{
    if(await bandClient.TileManager.AddTileAsync(tile))
    {
         ///console print something
    }
}
catch(BandException ex)
{
    //blabla handle
}

我认为问题可能是您将可写位图大小设置为(1,1)?

我有这样的方法:

public static class BandIconUtil
{
    public static async Task<BandIcon> FromAssetAsync(string iconFileName, int size = 24)
    {
        string uri = "ms-appx:///" + iconFileName;
        StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri, UriKind.RelativeOrAbsolute));
        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
        {
            WriteableBitmap bitmap = new WriteableBitmap(size, size);
            await bitmap.SetSourceAsync(fileStream);
            return bitmap.ToBandIcon();
        }
    }
}