2015-12-06 22 views
2

我想通过Windows Phone的UWP应用中的Microsoft Band SDK将自定义图块添加到Microsoft Band。这是我的示例代码。尝试从UWP应用向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。也许这可以帮助。

+1

你确定你尚未与最大的瓷砖填补你的乐队?顺便说一下,您正在测试哪个频段? –

+0

正如@SvenBorden所说的,您可能想要检查您是否有能力添加带状图块。 –

+0

斯文和詹姆斯你好,我也检查过我有乐队的能力。目前我有5个空插槽。我针对Microsoft Band 2开发了这个按钮。 –

回答

0

也许这会有所帮助,从MS乐队

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 
} 
+0

你好,我也试过这种方法,但它不会改变任何东西。我无法将自己的贴片添加到我的乐队中。 –

+0

你解决了你的问题吗?如果没有,你是从手机还是从你的电脑推动瓦片?你是否尝试重置乐队?或设置一个新的工作? –

+0

我想要先开始使用电话客户端。是的,我重新设置了乐队并设置了一个新项目。我已经用示例项目更新了我的问题。也许这可以帮助解决这个问题。 –

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(); 
     } 

    } 
} 
相关问题