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

时间:2015-12-06 11:32:34

标签: c# uwp microsoft-band windows-10-universal

我想在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应用程序中使用。

有什么想法吗?

更新 这是sample app as download。也许这会有所帮助。

2 个答案:

答案 0 :(得分:0)

也许这会有所帮助,来自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 :(得分:0)

我认为问题可能是您将可写位图大小设置为(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();
        }

    }
}