MS Band SDK-未调用按钮事件处理程序 [英] MS Band SDK - Button pressed event handler not being called

查看:96
本文介绍了MS Band SDK-未调用按钮事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在应用中使用按钮设置了图块和页面布局,但是当我按下按钮时,不会调用事件处理程序.我尝试了图块打开事件处理程序,但这也不起作用.我的代码如下:

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?

更新:我只是再次浏览了我的代码和SDK doco,并想起我正在做一些不同的事情,这就是为什么它可能无法正常工作的原因. doco具有以下用于将按钮添加到无法编译的布局的方法:

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!")
);

编译器说没有Button的构造函数带有2个参数.

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

我假设示例代码中有一个错误,并将其更改为TextButtonData,可以正常编译,但是现在我想知道这是否是事件处理程序不起作用的原因吗?代码是:

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"));

有什么想法吗?

推荐答案

只有在具有活动的IBandClient实例(即与Band的活动连接)时,才能从Band接收事件.在上面的代码中,由于使用了using() {}块,因此在调用StartReadingsAsync()之后立即丢弃了bandClient实例.放置IBandClient实例后,它将导致应用程序与Band断开连接.

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.

您需要在IBandClient实例上保持一段时间,以保持其希望接收事件的时间,并仅在该时间之后处置该实例.

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.

这篇关于MS Band SDK-未调用按钮事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆