2015-12-06 49 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(); 
     } 

    } 
} 
相關問題