我已经在我的应用程序中使用按钮设置了我的磁贴和页面布局,但是当我按下按钮时,不会调用事件处理程序。我尝试使用磁贴打开事件处理程序,但这也不起作用。我的代码如下:
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
}
磁贴和页面创建正常,但按钮不会触发事件处理程序。知道为什么不叫它吗?
更新:我刚刚再次浏览了我的代码和 SDK doco,并记得我正在做一些不同的事情,这就是为什么它可能不起作用的原因。文档具有以下内容,用于将按钮添加到无法编译的布局中:
// 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!”)
);
编译器说 Button 没有接受 2 个参数的构造函数。
我假设示例代码中存在错误,并将其更改为编译良好的 TextButtonData,但现在我想知道这是否是事件处理程序不起作用的原因?代码是:
PageData pageContent = new PageData(
pageGuid,
0, // index of our (only) layout
new TextButtonData(
(short)TilePageElementId.Button_PushMe, "Push"));
有什么想法吗?
很高兴看到有人在MS乐队上开发。这里有一些讨论OnConnectToBand及其设置的链接
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
}
第 9 部分 - 处理自定义事件http://developer.microsoftband.com/Content/docs/Microsoft%20Band%20SDK.pdf
讨论添加、单击、删除磁贴http://www.jayway.com/2015/03/04/first-impression-of-microsoft-band-developing-2/
尝试添加一个对话框(下面是Windows代码,对于iOS或Android,请查看上面提到的手册)来响应事件(在上面的代码中,事件处理程序中没有任何内容? 这看看它是否真的做了什么?
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
}
您只能在具有活动IBandClient
实例(即与 Band 的活动连接)时从 Band 接收事件。 在上面的代码中,由于使用了 using() {}
块,bandClient
实例在调用 StartReadingsAsync()
后立即被释放。 释放 IBandClient
实例时,会导致应用程序与 Band断开连接。
您需要在希望接收事件的时间段内保留IBandClient
实例,并仅在该时间之后释放该实例。