【发布时间】: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