我想通过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。也许可以帮上忙。

最佳答案

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

关于c# - 尝试从UWP应用将自定义磁贴添加到Microsoft Band,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34116945/

10-12 01:44