Search code examples
c#microsoft-band

MS Band SDK - Button pressed event handler not being called


I've set up my tile and page layout with a button in my app but when I press the button the event handler does not get called. I tried with the tile open event handler but that doesn't work either. My code is as follows:

private async void OnConnectToBand()
{

    IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();

    try
    {
        using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
        {

        //add tile, create page layout with button and add content with button

        //subscribe to listeners

        bandClient.TileManager.TileButtonPressed += EventHandler_TileButtonPressed;

        // Start listening for events 
        bandClient.TileManager.StartReadingsAsync();
        }
    }
    catch(BandException ex)
    { 
        //handle a Band connection exception 
    } 
}

void EventHandler_TileButtonPressed(object sender, BandTileEventArgs<IBandTileButtonPressedEvent> e)
{ 
// handle event
}

The tile and page get created fine but the button doesn't trigger the event handler. Any ideas why it's not being called?

UPDATE: I just went through my code and the SDK doco again and remembered I'm doing something different which is why it might not be working. The doco has the following for adding the button to the layout which doesn't compile:

// create the content to assign to the page 
PageData pageContent = new PageData
( 
pageGuid, 
0, // index of our (only) layout 
new Button( 
        TilePageElementId.Button_PushMe, 
        “Push Me!”)
);

The compiler says there isn't a constructor for Button that takes in 2 arguments.

I assumed there was an error in the sample code and changed it to TextButtonData which compiles fine but now I'm wondering if that is why the event handler isn't working? Code is:

PageData pageContent = new PageData( 
      pageGuid, 
      0, // index of our (only) layout 
      new TextButtonData(
         (short)TilePageElementId.Button_PushMe, "Push"));

Any ideas?


Solution

  • You can only receive events from the Band while you have an active IBandClient instance (i.e. an active connection to the Band). In your code above, the bandClient instance is disposed of immediately after StartReadingsAsync() is called, due to the use of the using() {} block. When an IBandClient instance is disposed, it causes the application to disconnect from the Band.

    You need to hold onto the IBandClient instance for the length of time during which you wish to receive events, and dispose of the instance only after that time.