0

I have referred the following link:

Silverlight for Windows Embedded

By referring this link i created a demo application which consist of two buttons created using Microsoft expression blend 2 tools. And then written a code referring the above site. Now my button names are "Browser Button" and "Media Button". On click of any one of the button i should able to launch the respective application. I was able to do for "Browser Button" but not for "Media Button" and if i do for "Media Button" then i am not able to do for "Browser Button".. I mean to say that how should i create event handler for both the buttons.

This is the code in c++ which i should modify

class BtnEventHandler
{
public:
    HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
    {
        RETAILMSG(1,(L"Browser event"));
        Execute(L"\\Windows\\iesample.exe",L"");
        return S_OK;
    }
};



// entry point for the application.
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
 LPWSTR lpCmdLine,int nCmdShow)
{
    PrintMessage();
    int      exitCode = -1;
    HRESULT  hr = S_OK;

if (!XamlRuntimeInitialize())
    return -1;

HRESULT retcode;
IXRApplicationPtr app;

if (FAILED(retcode=GetXRApplicationInstance(&app)))
    return -1;

if (FAILED(retcode=app->AddResourceModule(hInstance)))
    return -1;

XRWindowCreateParams wp;

ZeroMemory(&wp, sizeof(XRWindowCreateParams));

wp.Style       = WS_OVERLAPPED;
wp.pTitle      = L"Bounce Test";
wp.Left        = 0;
wp.Top         = 0;

XRXamlSource xamlsrc;

xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINTRESOURCE(IDR_XAML1));


IXRVisualHostPtr vhost;
if (FAILED(retcode=app->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))
    return -1;  

IXRFrameworkElementPtr root;    
if (FAILED(retcode=vhost->GetRootElement(&root)))
    return -1;  

IXRButtonBasePtr btn;   
if (FAILED(retcode=root->FindName(TEXT("BrowserButton"), &btn)))
    return -1;      

IXRDelegate<XRMouseButtonEventArgs>* clickdelegate;
BtnEventHandler handler;    

if(FAILED(retcode=CreateDelegate
    (&handler,&BtnEventHandler::OnClick,&clickdelegate)))
    return -1;
if (FAILED(retcode=btn->AddClickEventHandler(clickdelegate)))
    return -1;

UINT exitcode;
if (FAILED(retcode=vhost->StartDialog(&exitcode)))
    return -1;

return exitCode;
}

I have to add event handler for both the button so that on emulator whenever i click on any one of the button i should be able to launch the respective applications.

Thanks in advance

4

1 回答 1

2

您可以创建两个单独的函数作为每个按钮的处理程序。
如果您希望相同的处理程序识别按下了哪个按钮并采取相应措施,您可以阅读以下 MSDN 文章来演示这一点。


我没有尝试过,但您也可以使用源对象的IXRDependencyObject::GetName来了解按下了哪个按钮。

您的处理程序看起来像:

HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
{
    BSTR pName[50];

    source->GetName(pName);
    if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
    {
            RETAILMSG(1,(L"Browser event"));
            Execute(L"\\Windows\\iesample.exe",L"");

    }
    else if (_tcscmp(L"BrowserEvent", LPCWSTR(pName)) == 0)
    {
            /* Handle another button or element */
    }
    return S_OK;
}
于 2010-05-06T14:03:40.467 回答