MS Band SDK - Button pressed event handler not bei

2019-05-21 06:38发布

问题:

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?

回答1:

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.



回答2:

This is great to see someone developing on the MS Band.... heres a few links that discuss the OnConnectToBand and its setup

void EventHandler_TileButtonPressed(object sender,
BandTileEventArgs<IBandTileButtonPressedEvent> e)
{
 // This method is called when the user presses the
 // button in our tile’s layout.
 //
 // e.TileEvent.TileId is the tile’s Guid.
 // e.TileEvent.Timestamp is the DateTimeOffset of the event.
 // e.TileEvent.PageId is the Guid of our page with the button.
 // e.TileEvent.ElementId is the value assigned to the button
 // in our layout (i.e.,
 // TilePageElementId.Button_PushMe).
 //
 // handle the event
 }

Section 9- Handling custom events http://developer.microsoftband.com/Content/docs/Microsoft%20Band%20SDK.pdf

Talks about adding, clicking, removing tiles http://www.jayway.com/2015/03/04/first-impression-of-microsoft-band-developing-2/



回答3:

Try adding a dialog(below is windows code, for ios or android have a look at the above mentioned manual) to respond to the event (in your code above there is nothing in your event handler? this to see if it actually does something?

using Microsoft.Band.Notifications;

try
{
 // send a dialog to the Band for one of our tiles
 await bandClient.NotificationManager.ShowDialogAsync(tileGuid,
"Dialog title", "Dialog body");
}
catch (BandException ex)
{
// handle a Band connection exception
}