【问题标题】:Try to add custom tile to Microsoft Band from UWP app尝试从 UWP 应用将自定义磁贴添加到 Microsoft Band
【发布时间】:2016-03-11 01:47:07
【问题描述】:

我想在适用于 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。也许这会有所帮助。

【问题讨论】:

  • 你确定你还没有用最大的瓷砖填满你的乐队吗?顺便问一下,你测试的是哪个乐队?
  • 正如@SvenBorden 所说,您可能需要检查您是否有能力添加乐队瓷砖。
  • 您好 Sven 和 James,我还检查了我在乐队中是否有容量。目前我有 5 个空插槽。我针对 Microsoft Band 2 开发。
  • 该按钮是否与 SplitView 控件(UWP 的新控件)中的此单击事件处理程序相关联?
  • 您好 Phil,在此示例中它不在 SplitView 中,但我有时也尝试使用位于 SplitView 控件中的按钮。是否有任何已知问题。

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


【解决方案1】:

也许这会有所帮助,来自 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
}

【讨论】:

  • 您好,我也尝试过这种方法,但它并没有改变任何东西。我无法将自己的图块添加到我的乐队中。
  • 您的问题解决了吗?如果没有,您是从手机还是从计算机推送这些图块?你试过重置乐队吗?或者设置一个新的项目来工作?
  • 我想先从电话客户端开始。是的,我已经重置了乐队并设置了一个新项目。我已经用示例项目更新了我的问题。也许这可以帮助解决这个问题。
【解决方案2】:

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

    }
}

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多